Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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 ANDs the filters. I want to OR the filters to get union of individual querysets.

1 Answer

0 votes
by (40.7k points)

Try the code given below:

from django.db.models import Q

User.objects.filter(Q(income__gte=5000)|Q(income__isnull=True))

For more information you can refer to this DOCS

Browse Categories

...