I would like my categories to show all items in that category including descendant categories, so the parent category contains all items for subcategories.
I have tried adding this method to my Category class in models.py
def get_all_products(self):
# To display all items from all subcategories
return Product.objects.filter(category__in=Category.objects.get_descendants(include_self=True))
And added this to my template.html, but it does't work. What am I doing wrong?
<li>{{ product.product_name }}</li>
{% empty %}
<li>No items</li>
{% endfor %}
I've realised where I was going wrong, the correct code is:
def get_all_products(self):
# To display all items from all subcategories
return Product.objects.filter(category__in=self.get_descendants(include_self=True))
{% for product in product.get_all_products %}
<li>{{ product.product_name }}</li>
{% empty %}
<li>No items</li>
{% endfor %}