Opened 22 months ago
Last modified 22 months ago
#35126 closed Bug
forms.NullBooleanField's validation logic is surprising — at Version 1
| Reported by: | Jeremy Lainé | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | 5.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Reading NullBooleanField's code lead me to believe that it would clean "1" to True, and "0" to False", just like BooleanField does:
A simple field-level test works:
>>> from django import forms
>>> field = forms.NullBooleanField()
>>> field.clean("1")
True
>>> field.clean("0")
False
But using this in an actual form fails, note the difference between the BooleanField and NullBooleanField:
>>> class DemoForm(forms.Form):
... field_a = forms.BooleanField()
... field_b = forms.NullBooleanField()
...
>>> form = DemoForm({"field_a": "1", "field_b": "1"})
>>> form.is_valid()
>>> form.cleaned_data
{'field_a': True, 'field_b': None}
The problem is that by default NullBooleanField uses a NullBooleanSelect which mangles the submitted data for some obscure backwards-compatibility reason:
Note:
See TracTickets
for help on using tickets.