views:

149

answers:

1

Hi, I need to get aggregated value of two columns. So first multiple them together and then get theirs sum(). Code below naturallz does not work, it is just for clarification. Is it somehow possible or should I use raw SQL?

SomeModel.objects.filter(**something).aggregate(Sum('one_column' * 'another_col'))
+2  A: 

You don't need that much raw SQL using extra().

obj = SomeModel.objects.filter(**something).extra(
    select = {'total': 'SUM(one_column * another_column)'},
)
Baresi
It works, thanks a lot
yedpodtrzitko