2015-04-24 58 views
12

這是一個簡單的問題。我想知道這是否是相同的寫:Django Query __isnull = True或= None

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

比:

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

我使用Django 1.8

回答

12

他們是平等的:

>>> 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