I know this question has been posted multiple times but I still couldn't find a definite answer to this problem. So, here I go:
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)
Everything works fine except self.participants.count() doesn't work. Any idea what could be the problem. Any help much appreciated.