Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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')
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #django #order #foreign #key #count
ADD COMMENT
Topic
Name
8+9 =