我在Django以下問題..我在models.py保存有許多一對多在Django問題關係
class Proposal(models.Model):
#whatever....
credit =models.FloatField(null=True, blank=True)
def save():
#here credit is saved based on some calculation (succesfully)
class Author(models.Model):
#whatever....
balance = models.FloatField(null=True,blank=True)
proposal = models.ManyToManyField(Proposal,null=True,blank=True)
def save(self, force_insert=False, force_update=True):
to_add = sum(self.proposal.all().values_list('credit',flat=True)) # a sum of credits
self.balance = self.balance + to_add # or F(self.balance) + to_add, anyway
try:
super(Author, self).save(force_insert, force_update)
except IntegrityError:
super(Author, self).save(force_insert=True, force_update=False)
所以我創建管理員,並從筆者的建議內的作家,我「保存」提案對象,確定,信用成功保存,然後我保存作者,但沒有更新餘額。發生這種情況,如果只有我重新保存作者對象..
有什麼建議嗎?