2👍
✅
Assuming the objects attached to your categories are called Advert
s:
{% for categ in categs %}
{{ categ.name }} ({{ categ.advert_set.count }})
{% endfor %}
If you changed the attribute related_name
of your Adverts in the Category
ModelClass
in your models.py
file you’ll have to adjust advert_set
to the corresponding related_name
.
For more information about how to access related objects take a look at the docs.
Source:stackexchange.com