From 8477f2f692ddb6f60e3c62404e2a3bc1fcad0ce6 Mon Sep 17 00:00:00 2001 From: Mjumbe Wawatu Poe Date: Sat, 25 Aug 2012 20:08:35 -0400 Subject: [PATCH] Pass the serializer kwargs into any related_serializers. - Addresses issue #214 --- djangorestframework/resources.py | 2 +- djangorestframework/serializer.py | 5 +++-- djangorestframework/tests/serializer.py | 26 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 2d19887c4..09f1fa382 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -15,7 +15,7 @@ class BaseResource(Serializer): exclude = None def __init__(self, view=None, depth=None, stack=[], **kwargs): - super(BaseResource, self).__init__(depth, stack, **kwargs) + super(BaseResource, self).__init__(depth, stack, view=view, **kwargs) self.view = view self.request = getattr(view, 'request', None) diff --git a/djangorestframework/serializer.py b/djangorestframework/serializer.py index 9fb658758..6dd22790d 100644 --- a/djangorestframework/serializer.py +++ b/djangorestframework/serializer.py @@ -106,6 +106,7 @@ class Serializer(object): if depth is not None: self.depth = depth self.stack = stack + self.kwargs = kwargs def get_fields(self, obj): fields = self.fields @@ -188,8 +189,8 @@ class Serializer(object): stack = self.stack[:] stack.append(obj) - return related_serializer(depth=depth, stack=stack).serialize( - obj, **kwargs) + return related_serializer(depth=depth, stack=stack, + **self.kwargs).serialize(obj, **kwargs) def serialize_max_depth(self, obj): """ diff --git a/djangorestframework/tests/serializer.py b/djangorestframework/tests/serializer.py index 834a60d09..5e6a82520 100644 --- a/djangorestframework/tests/serializer.py +++ b/djangorestframework/tests/serializer.py @@ -3,6 +3,7 @@ from django.db import models from django.test import TestCase from django.utils.translation import ugettext_lazy from djangorestframework.serializer import Serializer +from djangorestframework.resources import Resource import datetime import decimal @@ -144,6 +145,31 @@ class TestFieldNesting(TestCase): self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}}) self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}}) + def test_serializer_nesting_inherits_kwargs(self): + """ + Test related model serializer inherits the parent serializer kwargs. + Specifically, tests that child resources will have the `view` as the + parent, addressing issue #214. + """ + class NestedM2(Resource): + fields = ('field1', ) + def field1(self, obj): + return self.view + obj.field1 + + class NestedM3(Resource): + fields = ('field2', ) + def field2(self, obj): + return self.view + obj.field2 + + class SerializerM2(Resource): + fields = [('field', NestedM2)] + + class SerializerM3(Resource): + fields = [('field', NestedM3)] + + self.assertEqual(SerializerM2(view=u'hello').serialize(self.m2), {'field': {'field1': u'hellofoo'}}) + self.assertEqual(SerializerM3(view=u'goodbye').serialize(self.m3), {'field': {'field2': u'goodbyebar'}}) + def test_serializer_overridden_hook_method(self): """ Test serializing a model instance which overrides a class method on the