Lee Hinde wrote:
After the line item is saved, I want to call the Work Order to have it update a Total Due column. Assuming I have the code in the Work Order model to get all the related line items and sum the extended price, what I would do is call the Work Order save() method from the Line Item save() methd, after calling the super(save).
Yes, I do this in almost exactly the same situation.
That is:
class LineItems(models.Model):
item = models.CharField(max_length=100)
wo = models.ForeignKey(WorkOrder)
def save(self):
doLocalMath()
super(LineItems, self).save()
WorkOrder.save()
This can be written as:
class LineItems(models.Model):
item = models.CharField(max_length=100)
wo = models.ForeignKey(WorkOrder)
qty = models.IntegerField(default=1)
price = models.DecimalField(max_digits=12, decimal_places=2)
ext_price = models.DecimalField(blank=True, max_digits=12,
decimal_places=2)
def save(self):
self.ext_price = self.qty * float(self.price)
super(LineItems, self).save()
self.wo.save()
And, sort of related, I need to call custom SQL to get the sum of related records, correct?
No, you can do it using the ORM:
class WorkOrder(models.Model):
total = models.DecimalField(max_digits=12, decimal_places=2)
def save(self):
self.total = sum([i.ext_price for i in self.lineitems_set.all()])
super(WorkOrder, self).save()
HTH
-Darryl
signature.asc
Description: OpenPGP digital signature

