I want to write a Django query equivalent to this SQL query:
SELECT * from user where income >= 5000 or income is NULL.
How to construct the Django queryset filter?
User.objects.filter(income__gte=5000, income=0)
This doesn't work, because it AND
s the filters. I want to OR
the filters to get union of individual querysets.
via Documentation
Because QuerySets implement the Python
__or__
operator (|
), or union, it just works. As you'd expect, the|
binary operator returns aQuerySet
soorder_by()
,.distinct()
, and other queryset filters can be tacked on to the end.