I'm using Django 1.7.8
I have models.py
class SaleDetail(models.Model):
quantity = models.PositiveSmallIntegerField(_('quantidade'))
price_sale = models.DecimalField(
def get_subtotal(self):
return self.price_sale * self.quantity
subtotal = property(get_subtotal)
./ manage.py shell
>>> from vendas_project.vendas.models import SaleDetail
>>> from django.db.models import Sum, F, FloatField
>>> q = SaleDetail.objects.values('price_sale', 'quantity').filter(sale=1)
>>> q.aggregate(Sum(F('price_sale') * F('quantity')), output_field=FloatField())
Generate error:
field_list = aggregate.lookup.split(LOOKUP_SEP)
AttributeError:
'ExpressionNode' object has no attribute 'split'
How do you calculate subtotals and totals in Django?
I need the result, eg:
price_sale quantity subtotal
10.50 2 21.00
9.55 3 28.65
total = 49.65