Django: filtering queryset by 'field__isnull=True' or 'field=None'? -
i have filter queryset dynamic value (which can none): may write:
filtered_queryset = queryset.filter(field=value)
or shall check none:
if value none: filtered_queryset = queryset.filter(field__isnull=true) else: filtered_queryset = queryset.filter(field=value)
does behaviour depend on particular dbms?
the orm handle none
(cast null) , return queryset
object, unless need catch none
input first example fine.
>>> user.objects.filter(username=none) [] >>> type(_) <class 'django.db.models.query.queryset'> >>> print user.objects.filter(username=none).query select "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" "auth_user" "auth_user"."username" null
Comments
Post a Comment