#Search query on Boolean not working for `None`
17 messages · Page 1 of 1 (latest)
If it's a boolean field, why are you trying to see if it's in [None] in the first place?
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 |
I have allowed blank and none in the field definition
Blank means nothing for a Boolean field, and having a nullable Boolean field is a little confusing
that's a new operator I'm coming across, let me see
I wanted a field with 3 states, true false and none. are there other alternatives?
I'd usually use an IntegerField with choices
oh we can limit with choices on integer fields
thanks!
how come =None works and in operator doesn't work, aren't they basically the same?
Print the SQL from each query and you'll see the difference
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
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.
is there a way to order boolean by True, None, False?
you could maybe use this: https://docs.djangoproject.com/en/5.1/ref/models/expressions/#using-f-to-sort-null-values
or use an annotation where based on the actual value you assign a number like 1-3 and then order_by the annotated field