Skip to content

Commit 6b4f45b

Browse files
committed
Fix some typos and little errors
1 parent bc12e0f commit 6b4f45b

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

docs/usage.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ field_name_mapping = {
429429

430430
#### Writing polymorphic resources
431431

432-
A polymorphic endpoint can be setup if associated with a polymorphic serializer.
433-
A polymorphic serializer take care of (de)serializing the correct instances types and can be defined like this:
432+
A polymorphic endpoint can be set up if associated with a polymorphic serializer.
433+
A polymorphic serializer takes care of (de)serializing the correct instances types and can be defined like this:
434434

435435
```python
436436
class ProjectSerializer(serializers.PolymorphicModelSerializer):
@@ -457,7 +457,7 @@ class CompanySerializer(serializers.ModelSerializer):
457457
model = models.Company
458458
```
459459

460-
They must be explicitely declared with the `polymorphic_serializer` (first positional argument) correctly defined.
460+
They must be explicitly declared with the `polymorphic_serializer` (first positional argument) correctly defined.
461461
It must be a subclass of `serializers.PolymorphicModelSerializer`.
462462

463463
<div class="warning">

example/serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from datetime import datetime
2-
from packaging import version
32

43
import rest_framework
54
from rest_framework_json_api import serializers, relations
5+
from packaging import version
66
from example.models import (
77
Blog, Entry, Author, AuthorBio, Comment, TaggedItem, Project, ArtProject, ResearchProject,
88
Company,

rest_framework_json_api/relations.py

+4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ def get_choices(self, cutoff=None):
226226

227227

228228
class PolymorphicResourceRelatedField(ResourceRelatedField):
229+
"""
230+
Inform DRF that the relation must be considered polymorphic.
231+
Takes a `polymorphic_serializer` as the first positional argument to retrieve then validate the accepted types set.
232+
"""
229233

230234
_skip_polymorphic_optimization = False
231235
default_error_messages = dict(ResourceRelatedField.default_error_messages, **{

rest_framework_json_api/serializers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ def __new__(cls, name, bases, attrs):
195195
type_to_serializer = {
196196
get_resource_type_from_serializer(serializer): serializer for
197197
serializer in polymorphic_serializers}
198-
setattr(new_class, '_poly_serializer_model_map', serializer_to_model)
199-
setattr(new_class, '_poly_model_serializer_map', model_to_serializer)
200-
setattr(new_class, '_poly_type_serializer_map', type_to_serializer)
201-
setattr(new_class, '_poly_force_type_resolution', True)
198+
new_class._poly_serializer_model_map = serializer_to_model
199+
new_class._poly_model_serializer_map = model_to_serializer
200+
new_class._poly_type_serializer_map = type_to_serializer
201+
new_class._poly_force_type_resolution = True
202202

203203
# Flag each linked polymorphic serializer to force type resolution based on instance
204204
for serializer in polymorphic_serializers:
205-
setattr(serializer, '_poly_force_type_resolution', True)
205+
serializer._poly_force_type_resolution = True
206206

207207
return new_class
208208

rest_framework_json_api/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def get_resource_name(context, expand_polymorphic_types=False):
7979
except AttributeError:
8080
try:
8181
serializer = view.get_serializer_class()
82-
if issubclass(serializer, PolymorphicModelSerializer) and \
83-
expand_polymorphic_types:
82+
if expand_polymorphic_types and issubclass(serializer, PolymorphicModelSerializer):
8483
return serializer.get_polymorphic_types()
8584
else:
8685
return get_resource_type_from_serializer(serializer)

0 commit comments

Comments
 (0)