Search code examples
djangodjango-modelsormdjango-orm

How to multiply to columns in different table and get another column as output in django orm


I have three tables :

  • First one is product table consist of product details and price
  • Second one is customer table with customer details
  • And a cart table with customer_id and product _id as foreign key and qty field for quantity of product

I want join cart table and product table and get an additional column called total price that is the result of price in product *qty in cart

How to do this in django orm

I tried f function but it didn't work


Solution

  • You can try this.

    Cart.objects.annotate(total_price=F('product__price')*F('qty'))