Elisa Elisa - 2 years ago 111
Python Question

How to perform OR condition in django queryset?

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 Djagno 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.

Answer Source
from django.db.models import Q
User.objects.filter(Q(income__gte=5000) | Q(income__isnull=True))

via Documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download