diff options
| author | Tom Christie | 2014-12-20 16:40:27 +0000 | 
|---|---|---|
| committer | Tom Christie | 2014-12-20 16:40:27 +0000 | 
| commit | ffb8d568e0cf8051fcdc3832143ba6bcc7c16f37 (patch) | |
| tree | 70551155f987252c072b4a71bcd4a5156282eed1 | |
| parent | d109ae0a2e78551977d93e2507267e43a685fafd (diff) | |
| parent | 77e3021fea3e30382b9770eac25371495e0b156b (diff) | |
| download | django-rest-framework-ffb8d568e0cf8051fcdc3832143ba6bcc7c16f37.tar.bz2 | |
Merge pull request #2330 from tomchristie/better-blank-html-behavior
Better behavior with null and '' for blank HTML fields.
| -rw-r--r-- | rest_framework/fields.py | 13 | ||||
| -rw-r--r-- | tests/test_fields.py | 22 | 
2 files changed, 20 insertions, 15 deletions
| diff --git a/rest_framework/fields.py b/rest_framework/fields.py index c40dc3fb..aab80982 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -273,7 +273,11 @@ class Field(object):                      return empty                  return self.default_empty_html              ret = dictionary[self.field_name] -            return self.default_empty_html if (ret == '') else ret +            if ret == '' and self.allow_null: +                # 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 +            return ret          return dictionary.get(self.field_name, empty)      def get_attribute(self, instance): @@ -545,8 +549,6 @@ class CharField(Field):          'min_length': _('Ensure this field has at least {min_length} characters.')      }      initial = '' -    coerce_blank_to_null = False -    default_empty_html = ''      def __init__(self, **kwargs):          self.allow_blank = kwargs.pop('allow_blank', False) @@ -560,11 +562,6 @@ class CharField(Field):              message = self.error_messages['min_length'].format(min_length=min_length)              self.validators.append(MinLengthValidator(min_length, message=message)) -        if self.allow_null and (not self.allow_blank) and (self.default is empty): -            # HTML input cannot represent `None` values, so we need to -            # forcibly coerce empty HTML values to `None` if `allow_null=True`. -            self.default_empty_html = None -      def run_validation(self, data=empty):          # Test for the empty string here so that it does not get validated,          # and so that subclasses do not need to handle it explicitly diff --git a/tests/test_fields.py b/tests/test_fields.py index 04c721d3..775d4618 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -223,8 +223,8 @@ class MockHTMLDict(dict):      getlist = None -class TestCharHTMLInput: -    def test_empty_html_checkbox(self): +class TestHTMLInput: +    def test_empty_html_charfield(self):          class TestSerializer(serializers.Serializer):              message = serializers.CharField(default='happy') @@ -232,23 +232,31 @@ class TestCharHTMLInput:          assert serializer.is_valid()          assert serializer.validated_data == {'message': 'happy'} -    def test_empty_html_checkbox_allow_null(self): +    def test_empty_html_charfield_allow_null(self):          class TestSerializer(serializers.Serializer):              message = serializers.CharField(allow_null=True) -        serializer = TestSerializer(data=MockHTMLDict()) +        serializer = TestSerializer(data=MockHTMLDict({'message': ''}))          assert serializer.is_valid()          assert serializer.validated_data == {'message': None} -    def test_empty_html_checkbox_allow_null_allow_blank(self): +    def test_empty_html_datefield_allow_null(self): +        class TestSerializer(serializers.Serializer): +            expiry = serializers.DateField(allow_null=True) + +        serializer = TestSerializer(data=MockHTMLDict({'expiry': ''})) +        assert serializer.is_valid() +        assert serializer.validated_data == {'expiry': None} + +    def test_empty_html_charfield_allow_null_allow_blank(self):          class TestSerializer(serializers.Serializer):              message = serializers.CharField(allow_null=True, allow_blank=True) -        serializer = TestSerializer(data=MockHTMLDict({})) +        serializer = TestSerializer(data=MockHTMLDict({'message': ''}))          assert serializer.is_valid()          assert serializer.validated_data == {'message': ''} -    def test_empty_html_required_false(self): +    def test_empty_html_charfield_required_false(self):          class TestSerializer(serializers.Serializer):              message = serializers.CharField(required=False) | 
