Search code examples
pythondjangodjango-oscar

How do you filter django-oscar products to show only products from a specific category?


I'm quite new to django-oscar and i'm trying to figure out how i can create a queryset that only returns products with a certain category on a section in my template, for instance all products with the category 'electronics'.How can i go about this?


Solution

  • Might be easiest to start with the category and then get all the category's products:

    from oscar.core.loading import get_model
    Category = get_model('catalogue', 'Category')
    
    cat = Category.objects.get(name='electronics')
    
    prods = cat.product_set.all()
    

    Then add prods to your response and use it in your template.