diff options
| author | Tom Christie | 2014-12-20 16:26:51 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-12-20 16:26:51 +0000 |
| commit | 77e3021fea3e30382b9770eac25371495e0b156b (patch) | |
| tree | a6ed8771a186b02a1eefd124808ead8189179e16 /rest_framework/fields.py | |
| parent | 80bacc5fb00682b589b3280c7082af73e3aaa8f8 (diff) | |
| download | django-rest-framework-77e3021fea3e30382b9770eac25371495e0b156b.tar.bz2 | |
Better behaviour with null and '' for blank HTML fields.
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 13 |
1 files changed, 5 insertions, 8 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 |
