Django Query __isnull=True or = None

Alejandro Veintimilla picture Alejandro Veintimilla · Apr 25, 2015 · Viewed 21.9k times · Source

this is a simple question. I'd like to know if it is the same to write:

queryset = Model.objects.filter(field=None)

than:

queryset = Model.objects.filter(field__isnull=True)

I'm using django 1.8

Answer

knbk picture knbk · Apr 25, 2015

They are equal:

>>> str(Person.objects.filter(age__isnull=True).query) == str(Person.objects.filter(age=None).query)
True
>>> print(Person.objects.filter(age=None).query)
SELECT "person_person"."id", "person_person"."name", "person_person"."yes", "person_person"."age" FROM "person_person" WHERE "person_person"."age" IS NULL
>>> print(Person.objects.filter(age__isnull=True).query)
SELECT "person_person"."id", "person_person"."name", "person_person"."yes", "person_person"."age" FROM "person_person" WHERE "person_person"."age" IS NULL

Exclusion: the Postgres JSON field (see the answer of @cameron-lee)