#Search query on Boolean not working for `None`

17 messages · Page 1 of 1 (latest)

mint lily
#

I have a search query for a boolen field with the __in operator. It works for values True and False but not None.
this works:```py
.filter(boolean_field__in=[True, False])

so does this:```py
.filter(boolean_field=None)

This doesn't work

.filter(boolean_field__in=[None])

What's the cause of this and how do I fix it?

tight elk
#

If it's a boolean field, why are you trying to see if it's in [None] in the first place?

last pumice
#

is it a nullable BooleanField? not the best idea usually, but you should be able to query for it via boolean_field__isnull=True and chain Q() objects via |

mint lily
tight elk
#

Blank means nothing for a Boolean field, and having a nullable Boolean field is a little confusing

mint lily
last pumice
mint lily
tight elk
#

I'd usually use an IntegerField with choices

mint lily
#

oh we can limit with choices on integer fields

mint lily
#

how come =None works and in operator doesn't work, aren't they basically the same?

tight elk
#

Print the SQL from each query and you'll see the difference

mint lily
#

it's gonna be a huge problem to switch to integer field now, I don't know how many places I have used boolean look ups

last pumice
#

it's just using boolean kind of carries the assumption you only ever have two options, not three, and many bugs can come from that wrong assumption in this case later on, not properly distinguishing the actual 3 options, especially with None/null being falsy as you pass it e.g. to Javascript. no need to refactor right away, but a choices field communicates this better and doesn't have that assumption baked into it.

mint lily
#

is there a way to order boolean by True, None, False?

last pumice