Search
 
SCRIPT & CODE EXAMPLE
 

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')
Comment

PREVIOUS NEXT
Code Example
Python :: python dictonary set default 
Python :: how to add to the end of an array python 
Python :: how to insert item at specifc index python 
Python :: generate binary number in python 
Python :: python yeild 
Python :: check setuptools version python 
Python :: remove multiple strings from list python 
Python :: gradient boosting regressor 
Python :: logging 
Python :: how to empty a dictionary in python 
Python :: merge lists 
Python :: python __str__ vs __repr__ 
Python :: pytohn reset all dictionary values to 0 
Python :: tryexept in python 
Python :: select python 
Python :: how to show a progress spinner when python script is running 
Python :: python substring from end 
Python :: selenium webdriver options python 
Python :: baeutifulsoup find element with text 
Python :: python plot horizontal line 
Python :: python get env 
Python :: pyinstaller onefile current working directory 
Python :: python switch case 3.10 Structural Pattern Matching 
Python :: subprocess.popen no output 
Python :: python unittest discover 
Python :: numpy linspace of dates 
Python :: np.r_ 
Python :: variable in regex python 
Python :: Flask command db migrate 
Python :: is coumn exist then delete in datafrmae 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =