Skip to content

Commit 40dc455

Browse files
committed
Bugfix for reverse relationships
1 parent 5c26912 commit 40dc455

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

rest_framework_json_api/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,14 @@ def get_related_resource_type(relation):
151151
parent_model = parent_serializer.parent.Meta.model
152152
parent_model_relation = getattr(
153153
parent_model,
154-
(parent_serializer.field_name if parent_serializer.field_name else relation.field_name)
154+
(relation.field_name if relation.field_name else parent_serializer.field_name)
155155
)
156-
relation_model = parent_model_relation.related.model
156+
if hasattr(parent_model_relation, 'related'):
157+
relation_model = parent_model_relation.related.model
158+
elif hasattr(parent_model_relation, 'field'):
159+
relation_model = parent_model_relation.field.related.model
160+
else:
161+
raise APIException('Unable to find related model for relation {relation}'.format(relation=relation))
157162
return inflection.pluralize(relation_model.__name__).lower()
158163

159164

0 commit comments

Comments
 (0)