Question

I have a model where I have a quantity, a price and a currency field:

quantity   price   currency
1          5       USD
2          3       USD
1          7       EUR  

I would like to sum the total price per currency. Basically, manually here is the result:

USD: 1*5  + 2*3 = 11
EUR: 1*7 = 7

I have tried several things with annotate and Sum but could not make it work. :/

Should I just use plain SQL to do that?

Thanks

Was it helpful?

Solution

Ok I think I finally found the solution:

items.values('currency').annotate(total=Sum('price', field='price*quantity'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top