Ticket #23547: 0001-fix-BooleanField-ticket-23547-in-case-when-field-is-.patch

File 0001-fix-BooleanField-ticket-23547-in-case-when-field-is-.patch, 2.5 KB (added by Lagovas, 10 years ago)
  • django/forms/fields.py

    From 6168382f1f2d038963ba4d70c356c375c7647fa5 Mon Sep 17 00:00:00 2001
    From: Dmitriy Korneev <lagovas.lagovas@gmail.com>
    Date: Wed, 24 Sep 2014 14:09:30 +0300
    Subject: [PATCH] fix BooleanField (ticket #23547) in case when field is
     required and have valid false value fix tests for BooleanField
    
    ---
     django/forms/fields.py                 | 4 +++-
     tests/forms_tests/tests/test_fields.py | 3 ---
     2 files changed, 3 insertions(+), 4 deletions(-)
    
    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 966b303..035055d 100644
    a b class BooleanField(Field):  
    696696        # will submit for False. Also check for '0', since this is what
    697697        # RadioSelect will provide. Because bool("True") == bool('1') == True,
    698698        # we don't need to handle that explicitly.
     699        self.raw_value = value
    699700        if isinstance(value, six.string_types) and value.lower() in ('false', '0'):
    700701            value = False
    701702        else:
    class BooleanField(Field):  
    703704        return super(BooleanField, self).to_python(value)
    704705
    705706    def validate(self, value):
    706         if not value and self.required:
     707        if self.raw_value in self.empty_values and self.required:
    707708            raise ValidationError(self.error_messages['required'], code='required')
     709        del self.raw_value
    708710
    709711    def _has_changed(self, initial, data):
    710712        # Sometimes data or initial could be None or '' which should be the
  • tests/forms_tests/tests/test_fields.py

    diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
    index 262ec5d..491f01e 100644
    a b class FieldsTests(SimpleTestCase):  
    850850        self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '')
    851851        self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None)
    852852        self.assertEqual(True, f.clean(True))
    853         self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, False)
    854853        self.assertEqual(True, f.clean(1))
    855         self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, 0)
    856854        self.assertEqual(True, f.clean('Django rocks'))
    857855        self.assertEqual(True, f.clean('True'))
    858         self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, 'False')
    859856
    860857    def test_booleanfield_2(self):
    861858        f = BooleanField(required=False)
Back to Top