Django Queryset __in no value in the list

a = M.objects.filter(f__in=[None, 1])
a.query.__str__()
u'SELECT * FROM "app_m" WHERE "app_m"."f" IN (None, 1)'

Don't you think it will be IN (NULL, 1)?

as:

a = M.objects.filter(f=None)
a.query.__str__()
u'SELECT * FROM "app_m" WHERE "app_m"."f" IS NULL'

Is this the default SQL behavior, django error, or am I missing something using f__in=?

Thank you in advance!

+5
source share
2 answers
a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 
+5
source

There seems to be an old bug in Django ( https://code.djangoproject.com/ticket/13768 ).

I just ran a few tests with Django 1.5, and it still exists: "None" is ignored when used in a list applied to "__in" (no errors).

Katherine's approach works like a charm :)

+1
source

All Articles