Django JSONField isnull 查找
Django JSONField isnull lookup
据我所知,您不能在 django 本机 JSONField
上使用 __isnull
查找。在 Internet 上,我发现了 this 个非活动问题。
作为可能的解决方法,我们当然可以使用这些技巧:
model.objects.filter(field__contains={'key': None})
,这不太灵活,因为您可能需要查询多个键或其他内容。
model.objects.exclude(field__key=True).exclude(field__key=False)
,这是 hacky,仅适用于布尔数据。
我希望有更好的方法 ((c) Raymond Hettinger) 来做到这一点。任何建议将不胜感激。现在,我将采用第一种方法
根据 this(请参阅最后的结束评论),以下内容应该有效 model.objects.filter(field__key=None)
(但显然 您应该使用带有修复程序的 Django 版本).
警告
Since any string could be a key in a JSON object, any lookup other
than those listed below will be interpreted as a key lookup. No errors
are raised. Be extra careful for typing mistakes, and always check
your queries work as you intend.
和here他们是
据我所知,您不能在 django 本机 JSONField
上使用 __isnull
查找。在 Internet 上,我发现了 this 个非活动问题。
作为可能的解决方法,我们当然可以使用这些技巧:
model.objects.filter(field__contains={'key': None})
,这不太灵活,因为您可能需要查询多个键或其他内容。model.objects.exclude(field__key=True).exclude(field__key=False)
,这是 hacky,仅适用于布尔数据。
我希望有更好的方法 ((c) Raymond Hettinger) 来做到这一点。任何建议将不胜感激。现在,我将采用第一种方法
根据 this(请参阅最后的结束评论),以下内容应该有效 model.objects.filter(field__key=None)
(但显然 您应该使用带有修复程序的 Django 版本).
警告
Since any string could be a key in a JSON object, any lookup other than those listed below will be interpreted as a key lookup. No errors are raised. Be extra careful for typing mistakes, and always check your queries work as you intend.
和here他们是