Opened 2 years ago

Closed 2 years ago

#22406 closed Bug (needsinfo)

Inconsistent way to parse boolean value from posted data

Reported by: xiaowl@… Owned by: nobody
Component: Forms Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


django.forms.fields.BooleanField.to_python tries to test if the given value is in ('false', '0'), but the value passed in is collected via BooleanField.widget.value_from_datadict, which uses a predefined dict {'true': True, 'false': False} to retreive boolean value, otherwise it returns bool(value).

Thus, posting a boolean field with value '0' will produce True, instead of False.

Attachments (1)

patch.diff (135 bytes) - added by xiaowl@… 2 years ago.

Download all attachments as: .zip

Change History (2)

Changed 2 years ago by xiaowl@…

comment:1 Changed 2 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

The fact that CheckboxInput.value_from_datadict treats 0 as True is by design (see #16820).
Please provide us with a more detailed use case to show why the above behavior is problematic for you.

Note: See TracTickets for help on using tickets.
Back to Top