python - how to include null results in Django ORM queries -


i using django orm system. have model of students each student has his/her prefered state of work. few students wanna work in "mahrashtra' state , others wanna work in other state.

let's suppose wanna search students have prefered working state 'california'. following query

result= students.object.filter(prefered_state='california') 

and of course desired result wanna include students in results haven't yet entered preferred state. means students state null or ''. isn't there way specify include null results criteria other using or statement. have many more criteria many other fields , don't wanna include or xyz null every field

you use q. note answer takes account both null values , empty strings.

from django.db.models import q  states = ['california', ''] item.objects.filter(q(prefered_state__in=states)|q(prefered_state__isnull=true)) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -