How to perform OR condition in django queryset? How to perform OR condition in django queryset? python python

How to perform OR condition in django queryset?

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

via Documentation

Because QuerySets implement the Python __or__ operator (|), or union, it just works. As you'd expect, the | binary operator returns a QuerySet so order_by(), .distinct(), and other queryset filters can be tacked on to the end.

combined_queryset = User.objects.filter(income__gte=5000) | User.objects.filter(income__isnull=True)ordered_queryset = combined_queryset.order_by('-income')

Update 2019-06-20: This is now fully documented in the Django 2.1 QuerySet API reference. More historic discussion can be found in DjangoProject ticket #21333.

Both options are already mentioned in the existing answers:

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


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

However, there seems to be some confusion regarding which one is to prefer.

The point is that they are identical on the SQL level, so feel free to pick whichever you like!

The Django ORM Cookbook talks in some detail about this, here is the relevant part:

queryset = User.objects.filter(        first_name__startswith='R'    ) | User.objects.filter(    last_name__startswith='D')

leads to

In [5]: str(queryset.query)Out[5]: 'SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login","auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name","auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff","auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user"WHERE ("auth_user"."first_name"::text LIKE R% OR "auth_user"."last_name"::text LIKE D%)'


qs = User.objects.filter(Q(first_name__startswith='R') | Q(last_name__startswith='D'))

leads to

In [9]: str(qs.query)Out[9]: 'SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name",  "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff",  "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user"  WHERE ("auth_user"."first_name"::text LIKE R% OR "auth_user"."last_name"::text LIKE D%)'

source: django-orm-cookbook