Pass the serializer kwargs into any related_serializers.

- Addresses issue #214
This commit is contained in:
Mjumbe Wawatu Poe 2012-08-25 20:08:35 -04:00
parent ef952c9b05
commit 8477f2f692
3 changed files with 30 additions and 3 deletions

View File

@ -15,7 +15,7 @@ class BaseResource(Serializer):
exclude = None exclude = None
def __init__(self, view=None, depth=None, stack=[], **kwargs): 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.view = view
self.request = getattr(view, 'request', None) self.request = getattr(view, 'request', None)

View File

@ -106,6 +106,7 @@ class Serializer(object):
if depth is not None: if depth is not None:
self.depth = depth self.depth = depth
self.stack = stack self.stack = stack
self.kwargs = kwargs
def get_fields(self, obj): def get_fields(self, obj):
fields = self.fields fields = self.fields
@ -188,8 +189,8 @@ class Serializer(object):
stack = self.stack[:] stack = self.stack[:]
stack.append(obj) stack.append(obj)
return related_serializer(depth=depth, stack=stack).serialize( return related_serializer(depth=depth, stack=stack,
obj, **kwargs) **self.kwargs).serialize(obj, **kwargs)
def serialize_max_depth(self, obj): def serialize_max_depth(self, obj):
""" """

View File

@ -3,6 +3,7 @@ from django.db import models
from django.test import TestCase from django.test import TestCase
from django.utils.translation import ugettext_lazy from django.utils.translation import ugettext_lazy
from djangorestframework.serializer import Serializer from djangorestframework.serializer import Serializer
from djangorestframework.resources import Resource
import datetime import datetime
import decimal import decimal
@ -144,6 +145,31 @@ class TestFieldNesting(TestCase):
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}}) self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}}) 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): def test_serializer_overridden_hook_method(self):
""" """
Test serializing a model instance which overrides a class method on the Test serializing a model instance which overrides a class method on the