Ticket #9609: mysql_compat.diff

File mysql_compat.diff, 2.7 KB (added by psagers, 6 years ago)
  • django/forms/fields.py

     
    582582        # Explicitly check for the string 'False', which is what a hidden field
    583583        # will submit for False. Because bool("True") == True, we don't need to
    584584        # handle that explicitly.
    585         if value == 'False':
     585        if value in ('False', '0'):
    586586            value = False
    587587        else:
    588588            value = bool(value)
     
    601601    def clean(self, value):
    602602        """
    603603        Explicitly checks for the string 'True' and 'False', which is what a
    604         hidden field will submit for True and False. Unlike the
    605         Booleanfield we also need to check for True, because we are not using
    606         the bool() function
     604        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.
    607607        """
    608         if value in (True, 'True'):
     608        if value in (True, 'True', '1'):
    609609            return True
    610         elif value in (False, 'False'):
     610        elif value in (False, 'False', '0'):
    611611            return False
    612612        else:
    613613            return None
  • tests/regressiontests/forms/fields.py

     
    10191019True
    10201020>>> f.clean(0)
    10211021False
     1022>>> f.clean('1')
     1023True
     1024>>> f.clean('0')
     1025False
    10221026>>> f.clean('Django rocks')
    10231027True
    10241028
     
    11431147>>> f.clean(False)
    11441148False
    11451149>>> f.clean(None)
     1150>>> f.clean('0')
     1151False
    11461152>>> f.clean('1')
     1153True
    11471154>>> f.clean('2')
    11481155>>> f.clean('3')
    11491156>>> f.clean('hello')
     
    11621169>>> f.cleaned_data['hidden_nullbool2']
    11631170False
    11641171
     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']
     1182True
     1183>>> f.cleaned_data['nullbool1']
     1184False
     1185>>> f.cleaned_data['nullbool2']
     1186
    11651187# MultipleChoiceField #########################################################
    11661188
    11671189>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])
Back to Top