[Django] #29618: using query parameters to filter by boolean causes assertion error due to lowercase true/false

4 views
Skip to first unread message

Django

unread,
Jul 31, 2018, 12:06:48 PM7/31/18
to django-...@googlegroups.com
#29618: using query parameters to filter by boolean causes assertion error due to
lowercase true/false
-----------------------------------------+------------------------
Reporter: Levi Schubert | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.0
Severity: Normal | Keywords: filter
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29618>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 31, 2018, 12:53:38 PM7/31/18
to django-...@googlegroups.com
#29618: using query parameters to filter by boolean causes validation error due to
lowercase true/false
-------------------------------+--------------------------------------
Reporter: levi schubert | Owner: nobody

Type: Bug | Status: new
Component: Uncategorized | Version: 2.0
Severity: Normal | Resolution:

Keywords: filter | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29618#comment:1>

Django

unread,
Jul 31, 2018, 3:08:48 PM7/31/18
to django-...@googlegroups.com
#29618: using query parameters to filter by boolean causes validation error due to
lowercase true/false
-------------------------------+--------------------------------------
Reporter: levi schubert | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 2.0
Severity: Normal | Resolution: invalid

Keywords: filter | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

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` or `False`
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’s `cleaned_data`
in order to filter your queryset.

There’s a bit of biolerplate involved in this. You might want to check out
[https://django-filter.readthedocs.io/en/master/ 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 big here please do follow-up with details and we
can re-open.

--
Ticket URL: <https://code.djangoproject.com/ticket/29618#comment:2>

Django

unread,
Jul 31, 2018, 4:12:39 PM7/31/18
to django-...@googlegroups.com
#29618: using query parameters to filter by boolean causes validation error due to
lowercase true/false
-------------------------------------+-------------------------------------
Reporter: levi schubert | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: filter | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Database layer (models, ORM)


Comment:

I believe the request is to allow
`Mode.objects.filter(booleanfield='true')` (see
[https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L997-L1007
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29618#comment:3>

Django

unread,
Aug 1, 2018, 3:14:47 AM8/1/18
to django-...@googlegroups.com
#29618: using query parameters to filter by boolean causes validation error due to
lowercase true/false

-------------------------------------+-------------------------------------
Reporter: levi schubert | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: filter | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

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
[https://github.com/django/django/blob/49f97b645fb1716cf69177558c41cf650a701eed/django/forms/fields.py#L701
`forms.BooleanField.to_python()`], with e.g.
[https://github.com/django/django/blob/49f97b645fb1716cf69177558c41cf650a701eed/django/forms/widgets.py#L518
`CheckboxInput.value_from_datadict()`]. The widget pulls the value from
the `GET` `QueryDict`, which is then passed to the field, before being
accessed via the form's `cleaned_data`.
* Django REST Framework serialisers: See [https://github.com/encode
/django-rest-
framework/blob/8493990a66e36d5dd4a62742d861b5b6b15cae80/rest_framework/fields.py#L682
`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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29618#comment:4>

Reply all
Reply to author
Forward
0 new messages