[Django]-Django: filtering by value in [None, other]

2πŸ‘

βœ…

It is based on SQL rules for execution. NULL values are not actual values to be compared. it’s an unknow value.

read more about this here: is null vs == null

In Django you can get records with NULL values like that:

MyElements.objects.filter(value__isnull = True)

So you need to first filter None out your list of comparisons and add it to your query by yourself.

from django.db.models import Q
MyElements.objects.filter(Q(value__in = [list_of_vals]) | Q(value__isnull = True))
πŸ‘€Ramy M. Mousa

1πŸ‘

You should test if a field is null using the isnull filter instead; otherwise in SQL any value compared to a null value would always evaluate as false:

from django.db.models import Q
MyElements.objects.filter(Q(value=myInt) | Q(value__isnull=True))
πŸ‘€blhsing

Leave a comment