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.
views:
36answers:
1
+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
2010-03-01 00:01:25
I think this counts the number of images instead of the number of tags...but I see where you're going.
Goose Bumper
2010-03-01 00:18:51
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
2010-03-01 00:20:28
I have modified my original code, sorry for mixing up the variables :)
WoLpH
2010-03-01 06:13:09