Django过滤器中的逻辑运算 [英] Logical operations in django filter
本文介绍了Django过滤器中的逻辑运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在以下位置获取我的查询集:
I need to get my queryset where:
(a="a" AND b=None) OR (a=None AND b="b")
我知道django中的Q对象,但是这种语法不起作用:
I know about Q objects in django, but this syntax doesn't works:
cls.objects.filter(models.Q(a="a", b=None) | models.Q(a=None, b="b"))
我绝对确定我的数据库包含预期的对象.但是我得到的只是空的查询集.
我想这里的语法有问题.但是在哪里?
例外的行图片.我只需要第一个和第二个
I am absolutely sure my database contains expected objects. But all I get is the empty queryset.
I guess there is some problem with syntax here. But where?
excepted rows picture. i need only first one and second one
推荐答案
cls.objects.filter(models.Q(a="a", b='None') | models.Q(a='None' AND b="b"))
尝试
这篇关于Django过滤器中的逻辑运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文