views:

36

answers:

1

I have two models, Image and Tag. Each Image object can have more than one Tag associated with it, and I want to find my most frequently used tags. How would I go about this? It seems easy enough but I can't seem to figure it out.

+1  A: 

Django has (only recently) acquired Aggregate support, so now you could do something like this:

from django.db.models import Count
Tag.objects.annotate(img_count=Count('image')).order_by('img_count')
WoLpH
I think this counts the number of images instead of the number of tags...but I see where you're going.
Goose Bumper
This solution very close to yours did the trick:Tag.objects.annotate(img_count=Count('image')).order_by('img_count')...Can you please update your answer and I'll mark it as correct.
Goose Bumper
I have modified my original code, sorry for mixing up the variables :)
WoLpH