1👍
✅
You can do this by using aggregation:
from django.db.models import Count
Category.objects.annotate(
entry_count=Count('entry_set')
).order_by('-entry_count')
Like this, you’ll get all the counts in one query and all the category objects will have entry_count
, so you can remove the @property
from the model.
Source:stackexchange.com