1
You’re right that you’ll have to do this in the view. You can use annotation to get the number of filtered articles for each subcategory:
from django.db.models import Count
def my_view(request, other, arguments):
...
subcategories = category.sub_category_set.filter(tag__tagname='xyz') \
.annotate(num_articles=Count('article__id'))
...
Pass subcategories
to your template context, and in your template you can do this:
{% for subcategory in subcategories %}
{{ subcategory.name }} -- {{ subcategory.num_articles }}
{% endfor %}
Source:stackexchange.com