5
I’m wearing Django 1.7.8
I have the 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())
Generates the error:
field_list = aggregate.lookup.split(LOOKUP_SEP)
AttributeError:
'ExpressionNode' object has no attribute 'split'
How you calculate subtotals and total in Django?
I need the result, example:
price_sale quantity subtotal
10.50 2 21.00
9.55 3 28.65
total = 49.65
That’s what you want is available on Django 1.8, but not in 1.7 or earlier. More than two years ago I had a similar problem (in the sense of having the same root cause), and posted that question on Soen, not yet answered. Unfortunately, I don’t know of any 1.8 pre-solution that doesn’t involve using
extra
(I can give you more details on an answer if you like).– mgibsonbr