diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 3f2e5a091..e6133d38d 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -15,8 +15,17 @@ class BaseResource(Serializer): def __init__(self, view=None, depth=None, stack=[], **kwargs): super(BaseResource, self).__init__(depth, stack, **kwargs) - self.view = view - self.request = getattr(view, 'request', None) + # If a view is passed, use that. Otherwise traverse up the stack + # to find a view we can use + if view is not None: + self.view = view + else: + for serializer in stack[::-1]: + if hasattr(serializer, 'view') \ + and getattr(serializer, 'view') != None: + self.view = getattr(serializer, 'view') + break + self.request = getattr(self.view, 'request', None) def validate_request(self, data, files=None): """ diff --git a/djangorestframework/serializer.py b/djangorestframework/serializer.py index 5dea37e81..4886420c9 100644 --- a/djangorestframework/serializer.py +++ b/djangorestframework/serializer.py @@ -100,6 +100,7 @@ class Serializer(object): def __init__(self, depth=None, stack=[], **kwargs): if depth is not None: self.depth = depth + stack.append(self) self.stack = stack def get_fields(self, obj): @@ -173,11 +174,11 @@ class Serializer(object): else: depth = self.depth - 1 + # detect circular references if any([obj is elem for elem in self.stack]): return self.serialize_recursion(obj) else: stack = self.stack[:] - stack.append(obj) return related_serializer(depth=depth, stack=stack).serialize(obj)