Django multipart ORM query including JOINs

66 Views Asked by At

I believe that I am simply failing to search correctly, so please redirect me to the appropriate question if this is the case.

I have a list of orders for an ecommerce platform. I then have two tables called checkout_orderproduct and catalog_product structured as:

|______________checkout_orderproduct_____________|
| id | order_id | product_id | qty | total_price |
--------------------------------------------------

|_____catalog_product_____|
| id | name | description |
---------------------------

I am trying to get all of the products associated with an order. My thought is something along the lines of:

for order in orders:
    OrderProduct.objects.filter(order_id=order.id, IM_STUCK_HERE)

What should the second part of the query be so that I get back a list of products such as

["Fruit", "Bagels", "Coffee"]
1

There are 1 best solutions below

3
On BEST ANSWER
products = (OrderProduct.objects
            .filter(order_id=order.id)
            .values('product_id'))
Product.objects.filter(id__in=products)

Or id__in=list(products): see note "Performance considerations" link.