[Answered ]-How to get 3rd level Object count in Django

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 }}.

Leave a comment