On 5/7/2009 4:28 PM, Andy Lei wrote: > I have 3 models. Something like this: > > Writer: > content = ManyToMany(Content) > Content > tags = ManyToMany(Tag) > Tag > > Now suppose I have a Tag, call it "tag1". The idea of the query is > that I want the Writers that have the most pieces of Content tagged > with tag1. > > Theoretically, it'd be something like > Writer.objects.annotate(content_count=Count > ("content__tags=tag1")).order_by('content_count") > > Obviously, that doesn't work. Any help would be greatly appreciated.
Hmm. I'm not sure if this is possible through the ORM. If it is, it's beyond me. The closest thing I can think of is: {{{ Writer.objects.filter(content__in=Content.objects.filter(tags=tag)) }}} Which will yield a QuerySet with each Writer repeated n times (once for each Content associated with that writer, tagged with `tag`). You should be able to do something in Python to get the Writer:count() mapping that you want. -- George --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---