Ticket #7500: bool.diff

File bool.diff, 1.7 KB (added by Alex Gaynor, 16 years ago)

This breaks one test because a test that used to raise "Value not in options" now raises "this field is required", not sure what the fix for that is.

  • django/newforms/fields.py

    diff --git a/django/newforms/fields.py b/django/newforms/fields.py
    index dfe46a2..e0b2199 100644
    a b __all__ = (  
    3636)
    3737
    3838# These values, if given to to_python(), will trigger the self.required check.
    39 EMPTY_VALUES = (None, '')
     39EMPTY_VALUES = (None, False, '')
    4040
    4141
    4242class Field(object):
    class BooleanField(Field):  
    535535
    536536    def clean(self, value):
    537537        """Returns a Python boolean object."""
     538        if value == 'False':
     539            value = False
    538540        super(BooleanField, self).clean(value)
    539541        # Explicitly check for the string 'False', which is what a hidden field
    540542        # will submit for False. Because bool("True") == True, we don't need to
    541543        # handle that explicitly.
    542         if value == 'False':
    543             return False
    544544        return bool(value)
    545545
    546546class NullBooleanField(BooleanField):
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index f3b6a96..207ff80 100644
    a b ValidationError: [u'This field is required.']  
    937937>>> f.clean(True)
    938938True
    939939>>> f.clean(False)
    940 False
     940Traceback (most recent call last):
     941...
     942ValidationError: [u'This field is required.']
    941943>>> f.clean(1)
    942944True
    943945>>> f.clean(0)
    944 False
     946Traceback (most recent call last):
     947...
     948ValidationError: [u'This field is required.']
    945949>>> f.clean('Django rocks')
    946950True
    947951
    948952>>> f.clean('True')
    949953True
    950954>>> f.clean('False')
    951 False
     955Traceback (most recent call last):
     956...
     957ValidationError: [u'This field is required.']
    952958
    953959>>> f = BooleanField(required=False)
    954960>>> f.clean('')
Back to Top