Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9decc1f

Browse files
committedAug 10, 2018
Improved related instance resolving
1 parent d9b5f08 commit 9decc1f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎rest_framework_json_api/views.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,18 @@ def get_related_field_name(self):
156156
return field_name
157157

158158
def get_related_instance(self):
159-
try:
160-
return getattr(self.get_object(), self.get_related_field_name())
161-
except AttributeError:
162-
raise NotFound
159+
parent_obj = self.get_object()
160+
parent_serializer = self.serializer_class(parent_obj)
161+
field_name = self.get_related_field_name()
162+
field = parent_serializer.fields.get(field_name, None)
163+
164+
if field is not None:
165+
return field.get_attribute(parent_obj)
166+
else:
167+
try:
168+
return getattr(parent_obj, field_name)
169+
except AttributeError:
170+
raise NotFound
163171

164172

165173
class ModelViewSet(AutoPrefetchMixin,

0 commit comments

Comments
 (0)
Please sign in to comment.