On Jun 17, 1:12 pm, R C <rileycr...@gmail.com> wrote: > Hi, > > I would like to be able to select objects and order them by the count > of a related field > > For example, I have 2 models: > > class Author(models.Model) > name = models.Charfield(max_length=20) > > class Article(models.Model) > author = models.ForeignKey(Author) > content = models.TextField() > > Let's say I have 3 authors: > 1) John -- has 10 articles > 2) Bob -- has 20 articles > 3) Lisa -- has 30 articles > > I would like to select authors and order by the number of articles > they have written: > Author.objects.all().order_by('article__count') > > But this does not work.
No, because you don't have a 'count' field in your article model. Read this (SVN version only): http://docs.djangoproject.com/en/dev/topics/db/aggregation/ -- DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---