2 django model save manytomanyfield
我知道这个问题已被多次发布,但我仍然找不到这个问题的明确答案.所以,我走了:
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)
Run Code Online (Sandbox Code Playgroud)
一切正常,除了self.participants.count()不起作用.知道可能是什么问题.任何帮助非常感谢.