diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-05-02 14:52:34 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-05-02 14:52:34 +0000 |
| commit | f6cca736a0e1a0ac19e65fb52fa224c1c0d258fd (patch) | |
| tree | a1899daef52a0f24fab500df43e41a30dbb06e7a /django/forms/fields.py | |
| parent | 95bcb70b5624a22cfc29795ea363e4a42fce6638 (diff) | |
Fixed #9609 -- Modified the clean method of(Null)Boolean field to accept '1' and '0' as valid inputs. Thanks to psagers for the patch.
This is required to support the use of non-default form widgets such as RadioSelect when the data comes from MySQL, which uses 1/0 to represent booleans.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/fields.py')
| -rw-r--r-- | django/forms/fields.py | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 3ff2819571..affe3879b9 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -586,9 +586,10 @@ class BooleanField(Field): def clean(self, value): """Returns a Python boolean object.""" # Explicitly check for the string 'False', which is what a hidden field - # will submit for False. Because bool("True") == True, we don't need to - # handle that explicitly. - if value == 'False': + # will submit for False. Also check for '0', since this is what + # RadioSelect will provide. Because bool("True") == bool('1') == True, + # we don't need to handle that explicitly. + if value in ('False', '0'): value = False else: value = bool(value) @@ -607,13 +608,13 @@ class NullBooleanField(BooleanField): def clean(self, value): """ Explicitly checks for the string 'True' and 'False', which is what a - hidden field will submit for True and False. Unlike the - Booleanfield we also need to check for True, because we are not using - the bool() function + 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 """ - if value in (True, 'True'): + if value in (True, 'True', '1'): return True - elif value in (False, 'False'): + elif value in (False, 'False', '0'): return False else: return None |
