Her iki seçenek de mevcut cevaplarda zaten belirtilmiştir:
from django.db.models import Q
q1 = User.objects.filter(Q(income__gte=5000) | Q(income__isnull=True))
ve
q2 = User.objects.filter(income__gte=5000) | User.objects.filter(income__isnull=True)
Ancak hangisinin tercih edileceği konusunda bazı karışıklıklar var gibi görünüyor.
Mesele şu ki , SQL düzeyinde aynıdırlar , bu yüzden istediğinizi seçmekten çekinmeyin!
Django ORM Cookbook burada, bu konuda bazı ayrıntılarıyla anlatıyor ilgili parçasıdır:
queryset = User.objects.filter(
first_name__startswith='R'
) | User.objects.filter(
last_name__startswith='D'
)
sebep olur
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%)'
ve
qs = User.objects.filter(Q(first_name__startswith='R') | Q(last_name__startswith='D'))
sebep olur
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%)'
Kaynak: django-orm-cookbook