This commit is contained in:
Warren Jin 2015-01-29 14:31:00 -05:00
parent 3b07c1dfc6
commit 3c7bd7f7f5

View File

@ -1296,7 +1296,7 @@ class RecursiveField(Field):
# This list of attributes determined by the attributes that # This list of attributes determined by the attributes that
# `rest_framework.serializers` calls to on a field object # `rest_framework.serializers` calls to on a field object
PROXIED_ATTRS = ( PROXIED_ATTRS = (
# bound fields # methods
'get_value', 'get_value',
'get_initial', 'get_initial',
'run_validation', 'run_validation',
@ -1313,13 +1313,22 @@ class RecursiveField(Field):
) )
def __init__(self, to=None, **kwargs): def __init__(self, to=None, **kwargs):
"""
arguments:
to - `None`, the name of another serializer defined in the same module
as this serializer, or the fully qualified import path to another
serializer. e.g. `ExampleSerializer` or
`path.to.module.ExampleSerializer`
"""
self.to = to self.to = to
self.kwargs = kwargs self.kwargs = kwargs
def bind(self, field_name, parent): def bind(self, field_name, parent):
if hasattr(parent, 'child') and parent.child is self: if hasattr(parent, 'child') and parent.child is self:
# RecursiveField nested inside of a ListField
parent_class = parent.parent.__class__ parent_class = parent.parent.__class__
else: else:
# RecursiveField directly inside a Serializer
parent_class = parent.__class__ parent_class = parent.__class__
if self.to is None: if self.to is None:
@ -1337,6 +1346,7 @@ class RecursiveField(Field):
raise ImportError( raise ImportError(
'could not locate serializer %s' % self.to, e) 'could not locate serializer %s' % self.to, e)
# Create a new serializer instance and proxy it
proxied = proxied_class(**self.kwargs) proxied = proxied_class(**self.kwargs)
proxied.bind(field_name, parent) proxied.bind(field_name, parent)
self.proxied = proxied self.proxied = proxied