Skip to content

Polymorphic list fixes #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Adam Wróbel <https://adamwrobel.com>
Adam Ziolkowski <[email protected]>
Christian Zosel <https://zosel.ch>
Greg Aker <[email protected]>
Jamie Bliss <[email protected]>
Expand All @@ -8,5 +9,6 @@ Matt Layman <https://www.mattlayman.com>
Ola Tarkowska <[email protected]>
Oliver Sauder <[email protected]>
Raphael Cohen <[email protected]>
Roberto Barreda <[email protected]>
santiavenda <[email protected]>
Yaniv Peer <[email protected]>
21 changes: 15 additions & 6 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework.serializers import BaseSerializer, ListSerializer, Serializer
from rest_framework.settings import api_settings

import rest_framework_json_api
from rest_framework_json_api import utils


Expand Down Expand Up @@ -543,12 +544,6 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

if serializer is not None:

# Get the serializer fields
fields = utils.get_serializer_fields(serializer)

# Determine if resource name must be resolved on each instance (polymorphic serializer)
force_type_resolution = getattr(serializer, '_poly_force_type_resolution', False)

# Extract root meta for any type of serializer
json_api_meta.update(self.extract_root_meta(serializer, serializer_data))

Expand All @@ -559,6 +554,17 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
resource = serializer_data[position] # Get current resource
resource_instance = serializer.instance[position] # Get current instance

if isinstance(serializer.child, rest_framework_json_api.
serializers.PolymorphicModelSerializer):
resource_serializer_class = serializer.child.\
get_polymorphic_serializer_for_instance(resource_instance)()
else:
resource_serializer_class = serializer.child

fields = utils.get_serializer_fields(resource_serializer_class)
force_type_resolution = getattr(
resource_serializer_class, '_poly_force_type_resolution', False)

json_resource_obj = self.build_json_resource_obj(
fields, resource, resource_instance, resource_name, force_type_resolution
)
Expand All @@ -573,6 +579,9 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
if included:
json_api_included.extend(included)
else:
fields = utils.get_serializer_fields(serializer)
force_type_resolution = getattr(serializer, '_poly_force_type_resolution', False)

resource_instance = serializer.instance
json_api_data = self.build_json_resource_obj(
fields, serializer_data, resource_instance, resource_name, force_type_resolution
Expand Down