【发布时间】:2020-07-11 08:15:07
【问题描述】:
我正在使用一个查询:
people = Person.objects.all().annotate(num_pets=Count('pets'))
for p in people:
print(p.name, p.num_pets == 0)
(Pet is ManyToOne with Person)
但实际上我对宠物的数量并不感兴趣,而只对一个人是否有宠物感兴趣。如何做到这一点?
【问题讨论】:
-
如注释,或过滤?
-
在注释中
-
你可以像
Person.objects.all().annotate(pets_exist=Exists('pets'))一样使用Exists