mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Resolve empty HTML charfield behavior. Closes #3318.
This commit is contained in:
parent
f601c6c1c3
commit
9ac646385f
|
@ -385,8 +385,10 @@ class Field(object):
|
|||
# If the field is blank, and null is a valid value then
|
||||
# determine if we should use null instead.
|
||||
return '' if getattr(self, 'allow_blank', False) else None
|
||||
elif ret == '' and self.default:
|
||||
return empty
|
||||
elif ret == '' and not self.required:
|
||||
# If the field is blank, and emptyness is valid then
|
||||
# determine if we should use emptyness instead.
|
||||
return '' if getattr(self, 'allow_blank', False) else empty
|
||||
return ret
|
||||
return dictionary.get(self.field_name, empty)
|
||||
|
||||
|
|
|
@ -253,7 +253,7 @@ class TestBooleanHTMLInput:
|
|||
|
||||
|
||||
class TestHTMLInput:
|
||||
def test_empty_html_charfield(self):
|
||||
def test_empty_html_charfield_with_default(self):
|
||||
class TestSerializer(serializers.Serializer):
|
||||
message = serializers.CharField(default='happy')
|
||||
|
||||
|
@ -261,6 +261,22 @@ class TestHTMLInput:
|
|||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'message': 'happy'}
|
||||
|
||||
def test_empty_html_charfield_without_default(self):
|
||||
class TestSerializer(serializers.Serializer):
|
||||
message = serializers.CharField(allow_blank=True)
|
||||
|
||||
serializer = TestSerializer(data=QueryDict('message='))
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'message': ''}
|
||||
|
||||
def test_empty_html_charfield_without_default_not_required(self):
|
||||
class TestSerializer(serializers.Serializer):
|
||||
message = serializers.CharField(allow_blank=True, required=False)
|
||||
|
||||
serializer = TestSerializer(data=QueryDict('message='))
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'message': ''}
|
||||
|
||||
def test_empty_html_integerfield(self):
|
||||
class TestSerializer(serializers.Serializer):
|
||||
message = serializers.IntegerField(default=123)
|
||||
|
|
Loading…
Reference in New Issue
Block a user