Skip to content

Commit 3077d0a

Browse files
TayHobbskhornberg
authored andcommitted
Enhancement: support naming API fields differently than model fields
1 parent 6091bdf commit 3077d0a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

rest_framework_json_api/renderers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,13 @@ def extract_included(fields, resource, resource_instance, included_resources):
263263
# For ManyRelatedFields if `related_name` is not set we need to access `foo_set` from `source`
264264
relation_instance_or_manager = getattr(resource_instance, field.child_relation.source)
265265
except AttributeError:
266-
if not hasattr(current_serializer, field.source):
266+
if hasattr(current_serializer, field.source):
267+
serializer_method = getattr(current_serializer, field.source)
268+
relation_instance_or_manager = serializer_method(resource_instance)
269+
elif hasattr(current_serializer.instance, field.source):
270+
relation_instance_or_manager = getattr(current_serializer.instance, field.source)
271+
else:
267272
continue
268-
serializer_method = getattr(current_serializer, field.source)
269-
relation_instance_or_manager = serializer_method(resource_instance)
270273

271274
new_included_resources = [key.replace('%s.' % field_name, '', 1)
272275
for key in included_resources

0 commit comments

Comments
 (0)