0👍
✅
I would use this:
def _total_amount(self):
req = QuoteRow.objects.filter(quote=self.pk)
return sum([i.unit_price * i.quantity for i in req])
It takes one more line but it’s clearer to me.
1👍
Try using extra()
QuoteRow.objects.filter(quote=self.pk).extra(select = {'total': 'SUM(unit_price * quantity)'},)
- Django Template getting apparent catchall error: "AssertionError at / No exception message supplied"
- Could my api+serializer code be improved?
- How to put several objects in one template
- How to access ManyToOneForeignKey data without making extra queries in Wagtail(django)
Source:stackexchange.com