Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#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: master
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


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/ 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/
--- django/forms/    (revision 3131)
+++ django/forms/    (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)


  • eric

Change History (1)

comment:1 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

(In [3161]) Fixed #2190 -- Now allowing NullBooleanField to be given None

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