Skip to content

support polymorphic list with proper type/fields resolution #372

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

Closed
wants to merge 7 commits into from
Closed
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
23 changes: 16 additions & 7 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.serializers import BaseSerializer, ListSerializer, Serializer
from rest_framework.settings import api_settings

from rest_framework_json_api import utils
from rest_framework_json_api import serializers, utils


class JSONRenderer(renderers.JSONRenderer):
Expand Down Expand Up @@ -535,12 +535,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 @@ -551,6 +545,18 @@ 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, 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 @@ -565,6 +571,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