[Django]-How to include null results in Django ORM queries

8👍

You could use Q. Note that this answer takes into account both NULL values and empty strings.

from django.db.models import Q

states = ['California', '']
Item.objects.filter(Q(prefered_state__in=states)|Q(prefered_state__isnull=True))

1👍

result= Students.object.filter(Q(prefered_state=’California’) | Q(prefered_state__isnull=True))

Check out Q object here:
https://docs.djangoproject.com/es/1.9/topics/db/queries/#complex-lookups-with-q-objects

0👍

from django.db.models import Q

result= Students.object.filter(Q(prefered_state='California')|Q(prefered_state__is_null=True))
👤Anoop

Leave a comment