Answer the question
In order to leave comments, you need to log in
QuerySet: how to sort by the number of records in the database?
Hello to all toasters!
There was one rather simple question: how to sort the records by the amount in the database when selecting from the database ? (I hasten to note that I'm doing this in Flask and half of the methods from the Django QuerySet documentation do not work)
There are users and they have a "Country" field. I make a selection of countries, but in this case the countries are not sorted.
countries = models.Users.objects.only('country').distinct('country')
Answer the question
In order to leave comments, you need to log in
As an option:
from django.db.models import Count
Users.objects.values('country').annotate(num=Count('country')).order_by('-num')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question