Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个模型,Image和Tag。每个 Image 对象可以有多个与之关联的 Tag,我想找到我最常用的标签。我该怎么办?这似乎很容易,但我似乎无法弄清楚。
Image
Tag
Django(仅在最近)获得了 Aggregate 支持,所以现在您可以执行以下操作:
from django.db.models import Count Tag.objects.annotate(img_count=Count('image')).order_by('img_count')