#2190 closed enhancement (fixed)
[patch] make the forms.NullBooleanField handle None value as forms.NullSelectField do
| Reported by: | nkeric | Owned by: | Adrian Holovaty |
|---|---|---|---|
| Component: | Validators | Version: | dev |
| 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 |
Description
I'm using the NullBooleanField for my Content model:
class Content(models.Model):
deleted = models.NullBooleanField(default=False, blank=True)
...
that creates the following field in db:
"deleted" bool NULL
I got KeyError while trying to create a new content record without the
new_data[\'deleted\'] beening set. it's caused by
django/forms/init.py line 574 (NullBooleanField):
573. def html2python(data):
574. return {'1': None, '2': True, '3': False}[data]
the "data" passed to this html2python is None if I didn't inject the
new_data[\'deleted\'] manually while posting.
Since NullBooleanField implies null-able value in the db level, I think
form.NullBooleanField should accept None value and handle it properly
as NullSelectField - if data is None, just return None. so here is the
simple patch:
Index: django/forms/__init__.py
===================================================================
--- django/forms/__init__.py (revision 3131)
+++ django/forms/__init__.py (working copy)
@@ -571,6 +571,8 @@
return SelectField.render(self, data)
def html2python(data):
+ if not data:
+ return None
return {'1': None, '2': True, '3': False}[data]
html2python = staticmethod(html2python)
regards,
- eric
(In [3161]) Fixed #2190 -- Now allowing NullBooleanField to be given None