Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9609 -- Modified the clean method of(Null)Boolean field to acc…

…ept '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.

Merge of r10660 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10662 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6a21ada37e353a00dc694441fdc0bcf309f0cc4c 1 parent 5a089a5
Russell Keith-Magee authored
17  django/forms/fields.py
@@ -585,9 +585,10 @@ class BooleanField(Field):
585 585
     def clean(self, value):
586 586
         """Returns a Python boolean object."""
587 587
         # Explicitly check for the string 'False', which is what a hidden field
588  
-        # will submit for False. Because bool("True") == True, we don't need to
589  
-        # handle that explicitly.
590  
-        if value == 'False':
  588
+        # will submit for False. Also check for '0', since this is what
  589
+        # RadioSelect will provide. Because bool("True") == bool('1') == True,
  590
+        # we don't need to handle that explicitly.
  591
+        if value in ('False', '0'):
591 592
             value = False
592 593
         else:
593 594
             value = bool(value)
@@ -606,13 +607,13 @@ class NullBooleanField(BooleanField):
606 607
     def clean(self, value):
607 608
         """
608 609
         Explicitly checks for the string 'True' and 'False', which is what a
609  
-        hidden field will submit for True and False. Unlike the
610  
-        Booleanfield we also need to check for True, because we are not using
611  
-        the bool() function
  610
+        hidden field will submit for True and False, and for '1' and '0', which
  611
+        is what a RadioField will submit. Unlike the Booleanfield we need to
  612
+        explicitly check for True, because we are not using the bool() function
612 613
         """
613  
-        if value in (True, 'True'):
  614
+        if value in (True, 'True', '1'):
614 615
             return True
615  
-        elif value in (False, 'False'):
  616
+        elif value in (False, 'False', '0'):
616 617
             return False
617 618
         else:
618 619
             return None
22  tests/regressiontests/forms/fields.py
@@ -1077,6 +1077,10 @@
1077 1077
 True
1078 1078
 >>> f.clean(0)
1079 1079
 False
  1080
+>>> f.clean('1')
  1081
+True
  1082
+>>> f.clean('0')
  1083
+False
1080 1084
 >>> f.clean('Django rocks')
1081 1085
 True
1082 1086
 
@@ -1201,7 +1205,10 @@
1201 1205
 >>> f.clean(False)
1202 1206
 False
1203 1207
 >>> f.clean(None)
  1208
+>>> f.clean('0')
  1209
+False
1204 1210
 >>> f.clean('1')
  1211
+True
1205 1212
 >>> f.clean('2')
1206 1213
 >>> f.clean('3')
1207 1214
 >>> f.clean('hello')
@@ -1220,6 +1227,21 @@
1220 1227
 >>> f.cleaned_data['hidden_nullbool2']
1221 1228
 False
1222 1229
 
  1230
+# Make sure we're compatible with MySQL, which uses 0 and 1 for its boolean
  1231
+# values. (#9609)
  1232
+>>> NULLBOOL_CHOICES = (('1', 'Yes'), ('0', 'No'), ('', 'Unknown'))
  1233
+>>> class MySQLNullBooleanForm(Form):
  1234
+...     nullbool0 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES))
  1235
+...     nullbool1 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES))
  1236
+...     nullbool2 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES))
  1237
+>>> f = MySQLNullBooleanForm({ 'nullbool0': '1', 'nullbool1': '0', 'nullbool2': '' })
  1238
+>>> f.full_clean()
  1239
+>>> f.cleaned_data['nullbool0']
  1240
+True
  1241
+>>> f.cleaned_data['nullbool1']
  1242
+False
  1243
+>>> f.cleaned_data['nullbool2']
  1244
+
1223 1245
 # MultipleChoiceField #########################################################
1224 1246
 
1225 1247
 >>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])

0 notes on commit 6a21ada

Please sign in to comment.
Something went wrong with that request. Please try again.