Skip to content

Commit bc12e0f

Browse files
committed
Remove the POLYMORPHIC_ANCESTOR code
1 parent 4eec4aa commit bc12e0f

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

docs/usage.md

-13
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,6 @@ field_name_mapping = {
427427

428428
### Working with polymorphic resources
429429

430-
#### Extraction of the polymorphic type
431-
432-
This package can defer the resolution of the type of polymorphic models instances to retrieve the appropriate type.
433-
However, most models are not polymorphic and for performance reasons this is only done if the underlying model is a subclass of a polymorphic model.
434-
435-
Polymorphic ancestors must be defined on settings like this:
436-
437-
```python
438-
JSON_API_POLYMORPHIC_ANCESTORS = (
439-
'polymorphic.models.PolymorphicModel',
440-
)
441-
```
442-
443430
#### Writing polymorphic resources
444431

445432
A polymorphic endpoint can be setup if associated with a polymorphic serializer.

example/settings/test.py

-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@
1515
REST_FRAMEWORK.update({
1616
'PAGE_SIZE': 1,
1717
})
18-
JSON_API_POLYMORPHIC_ANCESTORS = (
19-
'polymorphic.models.PolymorphicModel',
20-
)

rest_framework_json_api/renderers.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
362362
serializer_fields,
363363
serializer_resource,
364364
nested_resource_instance,
365-
resource_type
365+
resource_type,
366+
getattr(serializer, '_poly_force_type_resolution', False)
366367
)
367368
)
368369
included_data.extend(
@@ -384,7 +385,8 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
384385
included_data.append(
385386
cls.build_json_resource_obj(
386387
serializer_fields, serializer_data,
387-
relation_instance, relation_type)
388+
relation_instance, relation_type,
389+
getattr(field, '_poly_force_type_resolution', False))
388390
)
389391
included_data.extend(
390392
cls.extract_included(
@@ -426,9 +428,10 @@ def extract_root_meta(cls, serializer, resource):
426428
return data
427429

428430
@classmethod
429-
def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name):
431+
def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name,
432+
force_type_resolution=False):
430433
# Determine type from the instance if the underlying model is polymorphic
431-
if isinstance(resource_instance, utils.POLYMORPHIC_ANCESTORS):
434+
if force_type_resolution:
432435
resource_name = utils.get_resource_type_from_instance(resource_instance)
433436
resource_data = [
434437
('type', resource_name),
@@ -512,6 +515,9 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
512515
# Get the serializer fields
513516
fields = utils.get_serializer_fields(serializer)
514517

518+
# Determine if resource name must be resolved on each instance (polymorphic serializer)
519+
force_type_resolution = getattr(serializer, '_poly_force_type_resolution', False)
520+
515521
# Extract root meta for any type of serializer
516522
json_api_meta.update(self.extract_root_meta(serializer, serializer_data))
517523

@@ -523,7 +529,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
523529
resource_instance = serializer.instance[position] # Get current instance
524530

525531
json_resource_obj = self.build_json_resource_obj(
526-
fields, resource, resource_instance, resource_name
532+
fields, resource, resource_instance, resource_name, force_type_resolution
527533
)
528534
meta = self.extract_meta(serializer, resource)
529535
if meta:
@@ -538,7 +544,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
538544
else:
539545
resource_instance = serializer.instance
540546
json_api_data = self.build_json_resource_obj(
541-
fields, serializer_data, resource_instance, resource_name
547+
fields, serializer_data, resource_instance, resource_name, force_type_resolution
542548
)
543549

544550
meta = self.extract_meta(serializer, serializer_data)

rest_framework_json_api/serializers.py

+6
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ def __new__(cls, name, bases, attrs):
198198
setattr(new_class, '_poly_serializer_model_map', serializer_to_model)
199199
setattr(new_class, '_poly_model_serializer_map', model_to_serializer)
200200
setattr(new_class, '_poly_type_serializer_map', type_to_serializer)
201+
setattr(new_class, '_poly_force_type_resolution', True)
202+
203+
# Flag each linked polymorphic serializer to force type resolution based on instance
204+
for serializer in polymorphic_serializers:
205+
setattr(serializer, '_poly_force_type_resolution', True)
206+
201207
return new_class
202208

203209

rest_framework_json_api/utils.py

-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
ReverseGenericRelatedObjectsDescriptor as ReverseGenericManyToOneDescriptor # noqa: F401
5353
)
5454

55-
POLYMORPHIC_ANCESTORS = ()
56-
for ancestor in getattr(settings, 'JSON_API_POLYMORPHIC_ANCESTORS', ()):
57-
ancestor_class = import_class_from_dotted_path(ancestor)
58-
POLYMORPHIC_ANCESTORS += (ancestor_class,)
59-
6055

6156
def get_resource_name(context, expand_polymorphic_types=False):
6257
"""

0 commit comments

Comments
 (0)