[英]Django JSONField isnull lookup
As far as I know you can't use __isnull
lookups on django native JSONField
.据我所知,您不能在 Django 原生JSONField
上使用__isnull
查找。 On the Internet I found this inactive issue.在互联网上,我发现了这个无效的问题。 As possible workaround we can of course use these hacks:作为可能的解决方法,我们当然可以使用这些黑客:
model.objects.filter(field__contains={'key': None})
, which isn't so flexible since you might need to query multiple keys or whatever. model.objects.filter(field__contains={'key': None})
,这不是那么灵活,因为您可能需要查询多个键或其他任何东西。
model.objects.exclude(field__key=True).exclude(field__key=False)
, which is hacky and works only for boolean data. model.objects.exclude(field__key=True).exclude(field__key=False)
,这是hacky,仅适用于布尔数据。
I hope there is a better way ((c) Raymond Hettinger) of doing this.我希望有更好的方法 ((c) Raymond Hettinger) 来做这件事。 Any advises will be appreciated.任何建议将不胜感激。 For now, I will go with the first approach现在,我将采用第一种方法
According to this (see the last, closing comment) the following should work model.objects.filter(field__key=None)
(But obviously you should use the Django version with the fix ).根据这个(见最后一个,结束评论)以下应该工作model.objects.filter(field__key=None)
(但显然你应该使用 Django 版本和 fix )。
The django docs Django 文档
warn that警告
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.由于任何字符串都可能是 JSON 对象中的键,因此除下面列出的那些之外的任何查找都将被解释为键查找。 No errors are raised.没有错误被提出。 Be extra careful for typing mistakes, and always check your queries work as you intend.输入错误时要格外小心,并始终检查您的查询是否按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.