[Fixed]-Django order by count

26👍

The proper way to do this is with annotation. This will reduce the amount of database queries to 1, and ordering will be a simple order_by function:

from django.db.models import Count

cat_list = Category.objects.annotate(count=Count('project_set__id')).order_by('count')
👤knbk

Leave a comment