2014-09-25 104 views
0

我有两个型号。产品和类别。每个产品都有一个类别,而类别有一个slu。。现在,当你调用的URL '/catalog/adventure/',你应该看到与类别“冒险”所有产品的列表,当你写“行动”,你应该能看到动作的产品等按类别筛选产品

所以我做了一个ListView

class BookListView(ListView): 
    template_name = 'project/shop/product-list-view.html' 

    def get_queryset(self): 
     category = get_object_or_404(Category, path=self.kwargs['slug']) 

     products = Product.objects.all().filter() 

     return products 

我必须在.filter()中写什么?

回答

0

这将有助于在这里看到自己的模型,但假设产品有一个ForeignKey类别,它应该是这么简单:

products = Product.objects.all().filter(category=category) 
0

假设产品有一个ForeignKey类别,我宁愿建议使用落后的关系与related_name:

class Product(models.Model): 
    ... 
    category = models.ForeignKey(Category, related_name='products') 
    ... 

,并在您的视图:

products = category.products.all() 

无需过滤器。