【发布时间】:2010-10-31 19:02:59
【问题描述】:
我知道这个问题已被多次发布,但我仍然找不到这个问题的明确答案。所以,我开始了:
class Invoice(models.Model):
program = models.ForeignKey(Program)
customer = models.ForeignKey(Customer, related_name='invoices')
participants = models.ManyToManyField(Participant, related_name='participants_set')
subtotal = models.DecimalField(max_digits=10, decimal_places=2, default='0.00', blank=True, null=False)
pst = models.DecimalField("PST", max_digits=10, decimal_places=2, default='0.00', blank=True, null=False)
gst = models.DecimalField("GST", max_digits=10, decimal_places=2, default='0.00', blank=True, null=False)
total = models.DecimalField(max_digits=10, decimal_places=2, default='0.00', blank=True, null=False)
def save(self, **kwargs):
super(Invoice, self).save(**kwargs)
items = self.participants.count()
subtotal = Decimal(self.program.fee) * items
pst = self.program.is_pst and Decimal(PST)*subtotal or Decimal('0.00')
gst = self.program.is_gst and Decimal(GST)*subtotal or Decimal('0.00')
total = (subtotal + pst) + gst
self.subtotal = subtotal
self.pst = pst
self.gst = gst
self.total = total
super(Invoice, self).save(**kwargs)
一切正常,除了 self.participants.count() 不起作用。任何想法可能是什么问题。非常感谢任何帮助。
【问题讨论】:
-
什么版本的 Django?您对 self.participants.count() 的调用应该有效。
-
count 方法是引发异常,还是给出了错误的值?
标签: django model save manytomanyfield