Answers for "django order by foreign key count"

0

django order by foreign key count

# order_by count of foreign key relation
from django.db.models import Count
top_users = User.objects.filter(problem_user=False) \
                .annotate(num_submissions=Count('submission')) \ # adds column "num_submissions" in "top_users"
                .order_by('-num_submissions')
Posted by: Guest on August-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language