﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
2190	[patch] make the forms.NullBooleanField handle None value as forms.NullSelectField do	nkeric	Adrian Holovaty	"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"	enhancement	closed	Validators	dev	normal	fixed			Unreviewed	1	0	0	0	0	0
