aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/serializer.py
diff options
context:
space:
mode:
authorTom Christie2012-08-23 04:15:41 -0700
committerTom Christie2012-08-23 04:15:41 -0700
commitdb7d15d5d136a9b4dcf759f9d588006244bd4e91 (patch)
treefded0c4f458944c4c33e7a2d43f1a6f415509746 /djangorestframework/serializer.py
parentacbc2d176825c024fcb5745a38ef506e915c00a1 (diff)
parent2f9775c12d172199c2a915062bfba3a13f5cadc4 (diff)
downloaddjango-rest-framework-db7d15d5d136a9b4dcf759f9d588006244bd4e91.tar.bz2
Merge pull request #181 from flashingpumpkin/master
Maintain a reference to the parent/root serializer
Diffstat (limited to 'djangorestframework/serializer.py')
-rw-r--r--djangorestframework/serializer.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/djangorestframework/serializer.py b/djangorestframework/serializer.py
index 027c9daf..3f05903b 100644
--- a/djangorestframework/serializer.py
+++ b/djangorestframework/serializer.py
@@ -96,6 +96,11 @@ class Serializer(object):
"""
The maximum depth to serialize to, or `None`.
"""
+
+ parent = None
+ """
+ A reference to the root serializer when descending down into fields.
+ """
def __init__(self, depth=None, stack=[], **kwargs):
if depth is not None:
@@ -130,9 +135,12 @@ class Serializer(object):
# If an element in `fields` is a 2-tuple of (str, tuple)
# then the second element of the tuple is the fields to
# set on the related serializer
+
+ class OnTheFlySerializer(self.__class__):
+ fields = info
+ parent = getattr(self, 'parent') or self
+
if isinstance(info, (list, tuple)):
- class OnTheFlySerializer(self.__class__):
- fields = info
return OnTheFlySerializer
# If an element in `fields` is a 2-tuple of (str, Serializer)
@@ -150,8 +158,9 @@ class Serializer(object):
elif isinstance(info, str) and info in _serializers:
return _serializers[info]
- # Otherwise use `related_serializer` or fall back to `Serializer`
- return getattr(self, 'related_serializer') or Serializer
+ # Otherwise use `related_serializer` or fall back to
+ # `OnTheFlySerializer` preserve custom serialization methods.
+ return getattr(self, 'related_serializer') or OnTheFlySerializer
def serialize_key(self, key):
"""