aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework
diff options
context:
space:
mode:
authorTom Christie2014-12-18 10:36:52 +0000
committerTom Christie2014-12-18 10:36:52 +0000
commit87ac64e41b60a26e6711648b9935c70dc35738a8 (patch)
tree7d4964abf1fd9a8b4e88feceef767ff0753e441c /rest_framework
parentb8af83493fa8d8b404174e67c3e21e7c05e9fc25 (diff)
downloaddjango-rest-framework-87ac64e41b60a26e6711648b9935c70dc35738a8.tar.bz2
Fixes for behavior with empty HTML fields.
Diffstat (limited to 'rest_framework')
-rw-r--r--rest_framework/fields.py17
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