Python Django:GenericRelation为null的查询

Python Django:GenericRelation为null的查询,python,django,Python,Django,我想在模型中查询泛型关系字段不为空的实例(即,在下面的示例中,我正在查找document.count()>0的实例): 比如: Report.objects.filter(date__gte=twomonths).exclude(document__isnull=True) 不幸的是,这不起作用-查询返回没有“document”的对象(即,它返回document.count()为0的对象) 有没有办法查询泛型关系为空的实例?我相信您的问题中可能还存在一些矛盾。注意:“我正在查找document

我想在模型中查询泛型关系字段不为空的实例(即,在下面的示例中,我正在查找document.count()>0的实例):

比如:

Report.objects.filter(date__gte=twomonths).exclude(document__isnull=True)
不幸的是,这不起作用-查询返回没有“document”的对象(即,它返回document.count()为0的对象)


有没有办法查询泛型关系为空的实例?

我相信您的问题中可能还存在一些矛盾。注意:“我正在查找document.count()==0的实例”,然后是“不幸的是,这不起作用-查询返回没有“document”的对象(即,它返回document.count()为0的对象)”

如果您想要无文档的
报告
s,可以使用:

Report.objects.filter(document__isnull=True)
Report.objects.filter(document__isnull=False)

如果希望
报告
至少包含一个文档,可以使用:

Report.objects.filter(document__isnull=True)
Report.objects.filter(document__isnull=False)


这很有道理,但查询不起作用:FieldError:无法将关键字“content\u type”解析为字段。选择是:。列出的选项中没有一个内容类型或对象id正确。对不起,这是我的错误。看到了
genericorrelation
但想到了
GenericForeignKey
。但现在我很困惑。如果document.count()的位置不是
0
,那么“关系为空”是什么意思?我更新了问题。抱歉,我意识到我用了一种令人困惑的方式来表达它——请注意“排除”,所以实际上我想排除document.count()==0的情况。
Report.objects.exclude(document__isnull=True)