本文介绍了如何在django查询中执行OR条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个等价于这个SQL查询的Django查询:
I want to write a Django query equivalent to this SQL query:
SELECT * from user where income >= 5000 or income is NULL.
如何构建Djagno查询过滤器?
How to construct the Djagno queryset filter?
User.objects.filter(income__gte=5000, income=0)
这不起作用,因为它 AND
是过滤器。我想要 OR
过滤器来获取单个查询器的联合。
This doesn't work, because it AND
s the filters. I want to OR
the filters to get union of individual querysets.
推荐答案
from django.db.models import Q
User.objects.filter(Q(income__gte=5000) | Q(income__isnull=True))
这篇关于如何在django查询中执行OR条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!