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
- [Django]-Access Django Test Database
- [Django]-How to restrict access for staff users to see only their information in Django admin page?
- [Django]-How to filter a Django QuerySet's related fields' 'all' or 'none'
- [Django]-Django compressor fails to find any `compress` tags
0👍
from django.db.models import Q
result= Students.object.filter(Q(prefered_state='California')|Q(prefered_state__is_null=True))
- [Django]-Errors running geodjango based apps on heroku
- [Django]-How to iterate over several list at the same time?
- [Django]-How to update where the files are stored in Django app
- [Django]-How to add html classes to a Django template 'for-loop' dynamically?
Source:stackexchange.com