Limit blank string -> None to just be on relational fields

This commit is contained in:
Tom Christie 2014-10-08 17:03:14 +01:00
parent 5ead8dc89d
commit f7d43f530a
2 changed files with 7 additions and 5 deletions

View File

@ -110,7 +110,6 @@ class Field(object):
default_validators = []
default_empty_html = empty
initial = None
coerce_blank_to_null = True
def __init__(self, read_only=False, write_only=False,
required=None, default=empty, initial=empty, source=None,
@ -248,9 +247,6 @@ class Field(object):
self.fail('required')
return self.get_default()
if data == '' and self.coerce_blank_to_null:
data = None
if data is None:
if not self.allow_null:
self.fail('null')

View File

@ -1,5 +1,5 @@
from rest_framework.compat import smart_text, urlparse
from rest_framework.fields import Field
from rest_framework.fields import empty, Field
from rest_framework.reverse import reverse
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch, Resolver404
@ -31,6 +31,12 @@ class RelatedField(Field):
)
return super(RelatedField, cls).__new__(cls, *args, **kwargs)
def run_validation(self, data=empty):
# We force empty strings to None values for relational fields.
if data == '':
data = None
return super(RelatedField, self).run_validation(data)
def get_queryset(self):
queryset = self.queryset
if isinstance(queryset, QuerySet):