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
23 changes: 17 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,19 @@ 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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind moving False and the closing parenthesis all to this line?

False
)

json_resource_obj = self.build_json_resource_obj(
fields, resource, resource_instance, resource_name, force_type_resolution
)
Expand All @@ -573,6 +581,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