Skip to content

Commit c926a66

Browse files
adsizedmblayman
authored andcommitted
Polymorphic list fixes (#402)
* support polymorphic list Render polymorphic list of items with different fields * pep8 * fix polymorphic models serializer fix get_polymorphic_serializer_for_instance only for PolymorphicModelSerializer * fix typo * pep8 * fix circular import and pep8 * Fix import in renderers * Fix remaining flake8 issue * Fix code style issue in renderers. * Add Adam Ziolkowski and Roberto Barreda as Authors
1 parent 9d200f2 commit c926a66

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

AUTHORS

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Adam Wróbel <https://adamwrobel.com>
2+
Adam Ziolkowski <[email protected]>
23
Christian Zosel <https://zosel.ch>
34
Greg Aker <[email protected]>
45
Jamie Bliss <[email protected]>
@@ -8,5 +9,6 @@ Matt Layman <https://www.mattlayman.com>
89
Ola Tarkowska <[email protected]>
910
Oliver Sauder <[email protected]>
1011
Raphael Cohen <[email protected]>
12+
Roberto Barreda <[email protected]>
1113
santiavenda <[email protected]>
1214
Yaniv Peer <[email protected]>

rest_framework_json_api/renderers.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from rest_framework.serializers import BaseSerializer, ListSerializer, Serializer
1212
from rest_framework.settings import api_settings
1313

14+
import rest_framework_json_api
1415
from rest_framework_json_api import utils
1516

1617

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

544545
if serializer is not None:
545546

546-
# Get the serializer fields
547-
fields = utils.get_serializer_fields(serializer)
548-
549-
# Determine if resource name must be resolved on each instance (polymorphic serializer)
550-
force_type_resolution = getattr(serializer, '_poly_force_type_resolution', False)
551-
552547
# Extract root meta for any type of serializer
553548
json_api_meta.update(self.extract_root_meta(serializer, serializer_data))
554549

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

557+
if isinstance(serializer.child, rest_framework_json_api.
558+
serializers.PolymorphicModelSerializer):
559+
resource_serializer_class = serializer.child.\
560+
get_polymorphic_serializer_for_instance(resource_instance)()
561+
else:
562+
resource_serializer_class = serializer.child
563+
564+
fields = utils.get_serializer_fields(resource_serializer_class)
565+
force_type_resolution = getattr(
566+
resource_serializer_class, '_poly_force_type_resolution', False)
567+
562568
json_resource_obj = self.build_json_resource_obj(
563569
fields, resource, resource_instance, resource_name, force_type_resolution
564570
)
@@ -573,6 +579,9 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
573579
if included:
574580
json_api_included.extend(included)
575581
else:
582+
fields = utils.get_serializer_fields(serializer)
583+
force_type_resolution = getattr(serializer, '_poly_force_type_resolution', False)
584+
576585
resource_instance = serializer.instance
577586
json_api_data = self.build_json_resource_obj(
578587
fields, serializer_data, resource_instance, resource_name, force_type_resolution

0 commit comments

Comments
 (0)