Fix boolean checkboxes setting to False. Closes #3258. Closes #2776.

This commit is contained in:
Tom Christie 2015-08-13 09:56:03 +01:00
parent a3e64fbe0d
commit 18af181191
2 changed files with 17 additions and 11 deletions

View File

@ -280,9 +280,7 @@ class Field(object):
self.allow_null = allow_null self.allow_null = allow_null
if self.default_empty_html is not empty: if self.default_empty_html is not empty:
if not required: if default is not empty:
self.default_empty_html = empty
elif default is not empty:
self.default_empty_html = default self.default_empty_html = default
if validators is not None: if validators is not None:

View File

@ -227,19 +227,27 @@ class TestInvalidErrorKey:
class TestBooleanHTMLInput: class TestBooleanHTMLInput:
def setup(self):
class TestSerializer(serializers.Serializer):
archived = serializers.BooleanField()
self.Serializer = TestSerializer
def test_empty_html_checkbox(self): def test_empty_html_checkbox(self):
""" """
HTML checkboxes do not send any value, but should be treated HTML checkboxes do not send any value, but should be treated
as `False` by BooleanField. as `False` by BooleanField.
""" """
# This class mocks up a dictionary like object, that behaves class TestSerializer(serializers.Serializer):
# as if it was returned for multipart or urlencoded data. archived = serializers.BooleanField()
serializer = self.Serializer(data=QueryDict(''))
serializer = TestSerializer(data=QueryDict(''))
assert serializer.is_valid()
assert serializer.validated_data == {'archived': False}
def test_empty_html_checkbox_not_required(self):
"""
HTML checkboxes do not send any value, but should be treated
as `False` by BooleanField, even if the field is required=False.
"""
class TestSerializer(serializers.Serializer):
archived = serializers.BooleanField(required=False)
serializer = TestSerializer(data=QueryDict(''))
assert serializer.is_valid() assert serializer.is_valid()
assert serializer.validated_data == {'archived': False} assert serializer.validated_data == {'archived': False}