mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-28 12:34:00 +03:00
Pass the serializer kwargs into any related_serializers.
- Addresses issue #214
This commit is contained in:
parent
ef952c9b05
commit
8477f2f692
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user