如何选择Django中另一个模型类型引用最多的模型记录?

时间:2010-06-23 14:37:26

标签: django django-models

例如,给定模型A和模型B,其中A是唯一记录,B是具有A的外键的记录,您将如何获取一组A,其中集合中的那些被引用B至少n次?

1 个答案:

答案 0 :(得分:1)

使用annotations

from django.db.models import Count
A.objects.annotate(b_count=Count('b')).filter(b_count__gte=n)