我有一个自定义函数,当信用额被添加到CreditsHistory表时运行,该表由信号调用:
def update_credits(sender, instance=None, **kwargs):
if instance is None:
return
sum = CreditsHistory.objects.filter(member=instance.member).aggregate(Sum('credits_amount'))
instance.member.n_credits = sum['credits_amount__sum']
instance.member.save()
post_save.connect(update_credits, sender=CreditsHistory)
post_delete.connect(update_credits, sender=CreditsHistory)
每天为某些成员更新积分,但不会为获得新CreditHistory实例的所有成员更新成员模型中的n_credits字段。我想有一些时间发生,或者数据库无法处理多个成员的大CreditsHistory表的聚合..我该如何调试呢?
编辑:
class CreditsHistory(models.Model):
member = models.ForeignKey(Member)
credits_amount = models.IntegerField()
date = models.DateTimeField(auto_now=True, editable=False)
notes = models.TextField(blank=True, null=True)
class Meta:
ordering = ['-date']
verbose_name_plural = "Credit History"
def __unicode__(self):
return u'credits-history/%s' % self.member.user.username
@classmethod
def create(cls, member, credits_amount, date, notes):
creditshistory = cls(member=member, credits_amount=credits_amount, date=date, notes=notes)
return creditshistory