aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework
diff options
context:
space:
mode:
authorTom Christie2014-12-13 15:08:36 +0000
committerTom Christie2014-12-13 15:08:36 +0000
commitdd712a1c2620b5dc9ad8eef5ff78ef232feb12e8 (patch)
tree9ce4ada25479a200ecabe2205a61cac2c6f194cd /rest_framework
parent6158285856b4dee77640883cf3fde3407af9ff2a (diff)
parent4fb757146af8e805e30cfe4a8914c9fb7251b3fc (diff)
downloaddjango-rest-framework-dd712a1c2620b5dc9ad8eef5ff78ef232feb12e8.tar.bz2
Merge pull request #2267 from tomchristie/better-misconfigured-serializer-errors
Better errors when serializer has incorrectly named field.
Diffstat (limited to 'rest_framework')
-rw-r--r--rest_framework/fields.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 25122e14..205efd2f 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -274,7 +274,23 @@ class Field(object):
Given the *outgoing* object instance, return the primitive value
that should be used for this field.
"""
- return get_attribute(instance, self.source_attrs)
+ try:
+ return get_attribute(instance, self.source_attrs)
+ except (KeyError, AttributeError) as exc:
+ msg = (
+ 'Got {exc_type} when attempting to get a value for field '
+ '`{field}` on serializer `{serializer}`.\nThe serializer '
+ 'field might be named incorrectly and not match '
+ 'any attribute or key on the `{instance}` instance.\n'
+ 'Original exception text was: {exc}.'.format(
+ exc_type=type(exc).__name__,
+ field=self.field_name,
+ serializer=self.parent.__class__.__name__,
+ instance=instance.__class__.__name__,
+ exc=exc
+ )
+ )
+ raise type(exc)(msg)
def get_default(self):
"""