Django Queryset __in with None value in list

不想你离开。 提交于 2020-04-12 16:10:41

问题


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

dont you think that would be IN (NULL, 1) ?

like:

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

Is this a default SQL behavior, django bug or I am missing something with f__in= ?

thank you in advance!


回答1:


a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 



回答2:


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

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

Catherine approach works like a charm :)



来源:https://stackoverflow.com/questions/15365823/django-queryset-in-with-none-value-in-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!