How To Order A Queryset By Related Objects Count?
My models.py is currently set up as follows: class Topic(models.Model): topic = models.CharField(max_length = 50) def __str__(self): return self.topic class Comic
Solution 1:
You can do this:
from django.db.models import Count
# ...
Topic.objects.annotate(cc=Count('comic')).order_by('-cc')
Post a Comment for "How To Order A Queryset By Related Objects Count?"