标签: python sql django relational-algebra
我有两个模型,Image和Tag。每个Image对象都可以有多个与之关联的Tag,我想找到我最常用的标签。我该怎么做?这似乎很容易,但我似乎无法弄明白。
Image
Tag
答案 0 :(得分:1)
Django(最近才)获得了Aggregate支持,所以现在你可以这样做:
from django.db.models import Count Tag.objects.annotate(img_count=Count('image')).order_by('img_count')