过滤存在相关对象的Django对象 [英] Filter Django objects where related object exists

查看:77
本文介绍了过滤存在相关对象的Django对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个重复的问题,但我找不到任何其他的问题。我正在尝试获得有投诉的照片列表。我不能简单地收到投诉并处理相关照片 - 我需要一张照片的查询。

This is bound to be a duplicate question but I can't find any others. I'm trying to get a list of photos that have complaints. I can't simply get complaints and deal with the related photos - I need a queryset of photos.

这应该可以工作,但似乎不正确:

This should work but doesn't seem right:

Photo.objects.filter(complaint__id__gte=0)

这似乎不是最有效的方式:

This doesn't seem like the most efficient way:

Photo.objects.annotate(Count('complaint')).exclude(complaint__count=0)

有更好的方法吗? / p>

Is there a better way?

推荐答案

如何...

Photo.objects.filter(complaint__isnull = False)

来自 https://docs.djangoproject.com/en/dev/topics/db/queries/

这篇关于过滤存在相关对象的Django对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆