From 17e75d03f92cff7d1d5d4679a8a09b13e08f3264 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Tue, 29 Jul 2014 11:04:13 -0600 Subject: [PATCH] Fixed #23129 -- Added 'true' and 'false' to `NullBooleanField`. JavaScript serializations of forms will sometimes render the boolean values as the strings 'true' and 'false', in lower case. Rather than repeat boilerplate in the JavaScript to circumvent this, it seems reasonable to allow Django to understand the lower-case versions of the booleans. --- django/forms/fields.py | 12 +++++++----- tests/forms_tests/tests/test_fields.py | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index ab746e4971..9070963607 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -753,13 +753,15 @@ class NullBooleanField(BooleanField): def to_python(self, value): """ Explicitly checks for the string 'True' and 'False', which is what a - hidden field will submit for True and False, and for '1' and '0', which - is what a RadioField will submit. Unlike the Booleanfield we need to - explicitly check for True, because we are not using the bool() function + hidden field will submit for True and False, for 'true' and 'false', + which are likely to be returned by JavaScript serializations of forms, + and for '1' and '0', which is what a RadioField will submit. Unlike + the Booleanfield we need to explicitly check for True, because we are + not using the bool() function """ - if value in (True, 'True', '1'): + if value in (True, 'True', 'true', '1'): return True - elif value in (False, 'False', '0'): + elif value in (False, 'False', 'false', '0'): return False else: return None diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index 1a6286451e..2482fe0682 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -1000,6 +1000,8 @@ class FieldsTests(SimpleTestCase): self.assertEqual(None, f.clean('2')) self.assertEqual(None, f.clean('3')) self.assertEqual(None, f.clean('hello')) + self.assertEqual(True, f.clean('true')) + self.assertEqual(False, f.clean('false')) def test_nullbooleanfield_2(self): # Make sure that the internal value is preserved if using HiddenInput (#7753)