2
The general principle is to start from the object you want to retrieve, or in this case count. So, you need to start with ShopProduct and follow the relationships. In a view that would be:
ShopProduct.objects.filter(subcategory__category__section=my_section)
However, you can’t do this in the template, because you can’t call methods with parameters. So you’d need to define this as a method on the Section class, replacing my_section
with self
, then you can do (eg) {{ section.get_product_count }}
.
Source:stackexchange.com