aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
authorTom Christie2014-10-08 11:04:08 +0100
committerTom Christie2014-10-08 11:04:08 +0100
commit093febb91299e332c810de6a6b6aba57c2b16a91 (patch)
tree27308bda44cb5b0187e9e32aa4460dc9e547ba01 /rest_framework/fields.py
parent3fa4a1898aee0dabee951f81f790bb2da042ec81 (diff)
downloaddjango-rest-framework-093febb91299e332c810de6a6b6aba57c2b16a91.tar.bz2
Tests for relational fields
Diffstat (limited to 'rest_framework/fields.py')
-rw-r--r--rest_framework/fields.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 9d577c53..5fb0ec8d 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -1,7 +1,7 @@
from django import forms
from django.conf import settings
from django.core import validators
-from django.core.exceptions import ValidationError
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.utils import six, timezone
from django.utils.datastructures import SortedDict
from django.utils.dateparse import parse_date, parse_datetime, parse_time
@@ -54,6 +54,8 @@ def get_attribute(instance, attrs):
for attr in attrs:
try:
instance = getattr(instance, attr)
+ except ObjectDoesNotExist:
+ return None
except AttributeError as exc:
try:
return instance[attr]
@@ -108,6 +110,7 @@ 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,
@@ -245,6 +248,9 @@ 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')
@@ -413,6 +419,7 @@ class CharField(Field):
'blank': _('This field may not be blank.')
}
initial = ''
+ coerce_blank_to_null = False
def __init__(self, **kwargs):
self.allow_blank = kwargs.pop('allow_blank', False)