문제

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

도움이 되었습니까?

해결책

Ok I think I finally found the solution:

items.values('currency').annotate(total=Sum('price', field='price*quantity'))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top