Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting only the models related to a Django queryset
    primarykey
    data
    text
    <p>I don't have much experience with Django (I'm using 1.3) so I have the feeling on the back of my head that this is a dumb question... But anyway:</p> <p>I have models like this:</p> <pre><code>class User(models.Model): name = models.CharField() class Product(models.Model): name = models.CharField() public = models.BooleanField() class Order(models.Model): user = models.ForeignKey(User) product = models.ManyToManyField(Product, through='OrderProduct') class OrderProduct(models.Model): product = models.ForeignKey(Product) order = models.ForeignKey(Order) expiration = models.DateField() </code></pre> <p>And let's say I do some query like this</p> <pre><code>Product.objects.filter(order__status='completed', order__user____id=2) </code></pre> <p>So I'd get all the products that <code>User2</code> bought (let's say it's just <code>Product1</code>). Cool. But now I want the expiration for that product, but if I call <code>Product1.orderproduct_set.all()</code> I'm gonna get every entry of <code>OrderProduct</code> with <code>Product1</code>, but I just want the one returned from my queryset. I know I can just run a different query on OrderProducts, but that would be another hit on the database just to bring back data the query I ran before can already get. <code>.query</code> on it gives me:</p> <pre><code>SELECT "shop_product"."id", "shop_product"."name" FROM "shop_product" INNER JOIN "shop_orderproducts" ON ("shop_product"."id" = "shop_orderproducts"."product_id") INNER JOIN "shop_order" ON ("shop_orderproducts"."order_id" = "shop_order"."id") WHERE ("shop_order"."user_id" = 2 AND "shop_order"."status" = completed ) ORDER BY "shop_product"."ordering" ASC </code></pre> <p>If I could <code>SELECT *</code> instead of specific fields I'd have all the data that I need in one query. Is there anyway to build that query and get only the data related to it?</p> <p><strong>EDIT</strong> I feel I need to clarify some points, I'm sorry I haven't been clearer:</p> <ol> <li><p>I'm not querying against <code>OrderProduct</code> because some products are public and don't have to be bought but I still have to list them, and they'd not be returned by a query against <code>OrderProduct</code></p></li> <li><p>The result I'm expecting is a list of products, along with their Order data (in case they have it). In JSON, it'd look somewhat like this</p> <p>[{id: 1, order: 1, expiration: 2013-03-03, public: false}, {id: 1, order: , expiration: , public: true</p></li> </ol> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload