1
Equipment.objects.all()
total = Equipment.objects.aggregate(price_sum=Sum('price'))
total_price = total['price_sum']

Django newbie here. I have a decimal varible called total_price. What I want to do is multiply the value by a VAT value. Now I already have a VAT field already in my models which can store a VAT. I want to be able to multiply that VAT field with total_price. With the vat field, its job is just to store one vat value.

class Equipment(models.Model):
   price = models.DecimalField(max_digits = 12, decimal_places=2)
   vat = models.ForeignKey(VAT)
  • `schneck`'s answer doesn't work. see my answer [here][1] [1]: http://stackoverflow.com/questions/12165636/django-aggregation-summation-of-multiplication-of-two-fields/19888120#19888120 – sha256 Nov 10 '13 at 09:24

1 Answers1

0

Without having tested, does something like this work?

Equipment.objects.aggregate(total=Sum(F('price') * F('vat__<yourfield>')))
schneck
  • 10,556
  • 11
  • 49
  • 74
  • see here: https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model – schneck Sep 20 '11 at 12:03
  • I had a look in the Django again, and I was surprised that indeed it's not possible: https://code.djangoproject.com/ticket/14030 – schneck Nov 29 '13 at 08:00