#29618 closed Bug (invalid)
using query parameters to filter by boolean causes validation error due to lowercase true/false
Reported by: | levi schubert | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.0 |
Severity: | Normal | Keywords: | filter |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When attempting to filter by a BooleanField using query parameters in the url, django raises a validation error of "'false' value must be either True or False." due to the capitalization of booleans in python. A workaround has been to .title() the parameter request. I'm unsure if this is considered a bug or if this is normal activity that just requires the extra configuration to handle the filter.
Change History (4)
comment:1 by , 6 years ago
Summary: | using query parameters to filter by boolean causes assertion error due to lowercase true/false → using query parameters to filter by boolean causes validation error due to lowercase true/false |
---|
comment:2 by , 6 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 6 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
I believe the request is to allow Mode.objects.filter(booleanfield='true')
(see BooleanField.to_python()). I don't find the existing laxness of accepting 0, 1, 't', 'f', 'True', 'False', True, and False particularly compelling and I wouldn't advocate for being even more liberal in the accepted values.
comment:4 by , 6 years ago
Ah, it it does take 'False'
— my mistake. (That explains using title()
.) This shouldn't work IMO. (But I suppose we can't change that for BC reasons...?)
For reference:
- Django
forms
: See `forms.BooleanField.to_python()`, with e.g. `CheckboxInput.value_from_datadict()`. The widget pulls the value from theGET
QueryDict
, which is then passed to the field, before being accessed via the form'scleaned_data
. - Django REST Framework serialisers: See `BooleanField.to_internal_value()`. The mapping from the various input values to a bool is done in a single step, being made available by the serialiser's
validated_data
.
Either way, the input is sanitised and correctly cast to the boolean type before ever encountering the model field.
It’s hard to know from your description exactly what you’re doing but I’d guess you’re just taking the string value contained in
request.GET
and trying to filter the model field on that.If so, this won’t work, since you need to pass a boolean
True
orFalse
to a boolean field.I don’t understand the point about
title()
at all, since the string’False’
isn’t a boolean either.It would have been helpful if you’d have provided a complete reproduce and the exact error, plus traceback.
However, the correct approach here it to pass
request.GET
into a form in order to validate the raw (user submitted) data and convert it to appropriate types, before using the value from the form’scleaned_data
in order to filter your queryset.There’s a bit of biolerplate involved in this. You might want to check out Django Filter, that wraps this up.
I’m going to close this as a usage question. If I’ve missed something and there’s a reproducible bug here please do follow-up with details and we can re-open.