I have the class Invoice, which (simplified) has the following attributes:
class Invoice(models.Model)
number = models.CharField(verbose_name="Number", max_length=16)
issue_date = models.DateTimeField(verbose_name="Issue date", default=datetime.now)
total = models.FloatField(verbose_name="Total", blank=True, null=True)
And then, I have the class InvoiceLine, which represents the line/lines that a invoice can have:
class InvoiceLine(models.Model):
invoice = models.ForeignKey(Invoice, verbose_name="Invoice")
description = models.CharField(verbose_name="Description", max_length=64)
line_total = models.FloatField(verbose_name="Line total")
InvoiceLine is an inline of the Invoice, and what I want to achieve is that, when in the admin somebody save the invoice with its lines (one ore more) the total of the invoice is calculated. I've tried to do it by overriding the method save:
class Invoice(models.Model)
number = models.CharField(verbose_name="Number", max_length=16)
issue_date = models.DateTimeField(verbose_name="Issue date", default=datetime.now)
total = models.FloatField(verbose_name="Total", blank=True, null=True)
def save(self, *args, **kwargs):
invoice_lines = InvoiceLine.objects.filter(invoice=self.id)
self.total = 0
for line in invoice_lines:
self.total=self.total+line.line_total
super(Invoice, self).save(*args, **kwargs)
The problem is that when I add elements in the InvoiceLine, the first time I save and the functionsave is called, the new elements in the inline (InvoiceLine) aren't stored yet, so when I do InvoiceLine.objects.filter(invoice=self.id)
, they are not taken into account. So, the only way this works is saving twice.
I've also tried:
def save(self, *args, **kwargs):
super(Invoice, self).save(*args, **kwargs)
invoice_lines = InvoiceLine.objects.filter(invoice=self.pk)
self.total = 0
for line in invoice_lines:
self.total=self.total+line.line_total
super(Invoice, self).save(*args, **kwargs)
But has the same result. Any idea? Thanks in advance!