2012-07-13 123 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()