1👍
✅
Try annotating the users with the user activity count.
from django.db.models import Count
User.objects.annotate(num_activities=Count('useractivity')).order_by('num_activities')
This will return a queryset of users, each with an attribute num_activities
. See the docs on annotation for more information.
Source:stackexchange.com