I need to calculate the sumproduct of two fields from Django queryset.
I have checked the answer in Django Aggregation: Summation of Multiplication of two fields but it doesnt work. They suggest the following code for django < 1.8:
from django.db.models import Sum
MyModel.objects.filter(<filters>).aggregate(Sum('field1', field="field1*field2"))
which returns the sum of the 'field1' not the defined field="field1*field2"
and for django >= 1.8
from django.db.models import Sum, F
MyModel.objects.filter(<filters>).aggregate(Sum(F('field1')*F('field2')))
which returns TypeError: Complex aggregates require an alias