[Answered ]-Django how to count 10 most occurrences

2👍

You should use aggregation:

from django.db.models import Count
rating = Liker.objects.values('pic_owners') \
                      .annotate(Count('pic_owners')) \
                      .order_by('-pic_owners__count')[:10]

This query will return a list of dicts with two keys: ‘pic_owners’ and ‘pic_owners__count’.

Leave a comment