diff options
| author | Tom Christie | 2014-12-18 10:55:28 +0000 | 
|---|---|---|
| committer | Tom Christie | 2014-12-18 10:55:28 +0000 | 
| commit | 22ffeafc3e67295d93b5f5d6dcb98917237d29eb (patch) | |
| tree | 29d246a361c8c4f224450cc0b4771ef8ef35b579 /rest_framework | |
| parent | e8b46413a769d04ffac2c35bb10a9556d242022c (diff) | |
| parent | 1087ccbb258ca79ee42509abc4bb17b6c277f9ce (diff) | |
| download | django-rest-framework-22ffeafc3e67295d93b5f5d6dcb98917237d29eb.tar.bz2 | |
Merge pull request #2311 from tomchristie/allow-empty-text-or-boolean-html-input-if-not-required
Fixes for behavior with empty HTML fields.
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/fields.py | 17 | 
1 files changed, 10 insertions, 7 deletions
| diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 5be2a21b..c40dc3fb 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -184,13 +184,11 @@ class Field(object):          self.style = {} if style is None else style          self.allow_null = allow_null -        if allow_null and self.default_empty_html 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 - -        if default is not empty: -            self.default_empty_html = default +        if self.default_empty_html is not empty: +            if not required: +                self.default_empty_html = empty +            elif default is not empty: +                self.default_empty_html = default          if validators is not None:              self.validators = validators[:] @@ -562,6 +560,11 @@ 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 | 
