2012-07-13 118 views
0

我有兩個型號Django模型過濾多對多

class Market(Model): 
    title=models.Charfield() 


class Product(Model): 
    title=Models.Charfield() 
    markets=ManyToMany(Market) 

而且我想在我看來,產品清單。我想爲每個市場獲得一種產品。 怎麼辦?

product_for_each_market_list=Products.objects.filter()# should be rather simple.. but i can't figure what to do 

謝謝。

+2

只是一個提示'market.product_set.all()[0]'。 – aisbaa 2012-07-13 08:18:29

+0

是否可以通過查詢產品來獲取我想要的內容? – Feanor 2012-07-13 08:22:09

+0

你可以在sql中顯示查詢嗎? – aisbaa 2012-07-13 10:05:56

回答

2

我不知道是多麼沉重,這將是,但是這是你可以用什麼:

Products.objects.filter(markets__in=Market.objects.all()).distinct()