Django sum of product query on joined table -


in nutshell wish know how express following sql query in django

select sum(product.cost_price * product.markup_percentage) product  inner join shopping_cart on shopping_cart.product_id = product.id  shopping_cart.user_id = <user_id>; 

there shoppingcart model has fk product model. trying fetch total amount paid user product in shopping cart.

at present adding column product table not option.

i know can do

shopping_cart = shoppingcart.objects.select_related('product').filter(user_id=<user_id>) 

to retrieve list of products , perform sum of product operation in python code.

the answer suggested in sumproduct using django's aggregation not work mentioned in comment answer.

so other writing raw sql query or doing calculation in python there other way in django?

does other answer work if change this?

.extra(     select={'actual_price': 'sum(cost_price * markup_percentage)'}, ).aggregate(total=sum('actual_price'))['total'] 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -