Ticket #9609: mysql_compat.diff
File mysql_compat.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/forms/fields.py
582 582 # Explicitly check for the string 'False', which is what a hidden field 583 583 # will submit for False. Because bool("True") == True, we don't need to 584 584 # handle that explicitly. 585 if value == 'False':585 if value in ('False', '0'): 586 586 value = False 587 587 else: 588 588 value = bool(value) … … 601 601 def clean(self, value): 602 602 """ 603 603 Explicitly checks for the string 'True' and 'False', which is what a 604 hidden field will submit for True and False. Unlike the605 Booleanfield we also need to check for True, because we are not using606 the bool() function604 hidden field will submit for True and False. Also checks for '1' and '0' 605 for MySQL compatibility. Unlike the Booleanfield we also need to check 606 for True, because we are not using the bool() function. 607 607 """ 608 if value in (True, 'True' ):608 if value in (True, 'True', '1'): 609 609 return True 610 elif value in (False, 'False' ):610 elif value in (False, 'False', '0'): 611 611 return False 612 612 else: 613 613 return None -
tests/regressiontests/forms/fields.py
1019 1019 True 1020 1020 >>> f.clean(0) 1021 1021 False 1022 >>> f.clean('1') 1023 True 1024 >>> f.clean('0') 1025 False 1022 1026 >>> f.clean('Django rocks') 1023 1027 True 1024 1028 … … 1143 1147 >>> f.clean(False) 1144 1148 False 1145 1149 >>> f.clean(None) 1150 >>> f.clean('0') 1151 False 1146 1152 >>> f.clean('1') 1153 True 1147 1154 >>> f.clean('2') 1148 1155 >>> f.clean('3') 1149 1156 >>> f.clean('hello') … … 1162 1169 >>> f.cleaned_data['hidden_nullbool2'] 1163 1170 False 1164 1171 1172 # Make sure we're compatible with MySQL, which uses 0 and 1 for its boolean 1173 # values. 1174 >>> NULLBOOL_CHOICES = (('1', 'Yes'), ('0', 'No'), ('', 'Unknown')) 1175 >>> class MySQLNullBooleanForm(Form): 1176 ... nullbool0 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) 1177 ... nullbool1 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) 1178 ... nullbool2 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) 1179 >>> f = MySQLNullBooleanForm({ 'nullbool0': '1', 'nullbool1': '0', 'nullbool2': '' }) 1180 >>> f.full_clean() 1181 >>> f.cleaned_data['nullbool0'] 1182 True 1183 >>> f.cleaned_data['nullbool1'] 1184 False 1185 >>> f.cleaned_data['nullbool2'] 1186 1165 1187 # MultipleChoiceField ######################################################### 1166 1188 1167 1189 >>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])