-
Notifications
You must be signed in to change notification settings - Fork 299
Support polymorphic models #211
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
Changes from 1 commit
dd4d4ec
8c73d95
681b5aa
96cbab0
5c63425
fddb06b
22829d1
d565334
e840438
19b0238
0ddf5ca
b8bf612
8fd4617
275793c
a26df13
2278976
8563b65
4aaeac2
030f6c8
ca23885
ae759e5
37c5ae6
f36821b
4eec4aa
bc12e0f
6b4f45b
36f3b6a
05cdb51
c1afe35
8ff5465
35c90d4
c5599c0
8d94efb
89ad607
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,7 +425,9 @@ field_name_mapping = { | |
|
||
### Working with polymorphic resources | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This jumps straight into how to use polymorphic resources. While that is appropriate for a Usage page, if this is the only place in the documentation that describes polymorphic resources, I think an introductory paragraph providing context and why a user would need this would be beneficial. Also, are there going to be related packages that are needed for this work? If so, can we link to them? I'm thinking about |
||
This package can defer the resolution of the type of polymorphic models instances to get the appropriate type. | ||
#### Extraction of the polymorphic type | ||
|
||
This package can defer the resolution of the type of polymorphic models instances to retrieve the appropriate type. | ||
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. | ||
|
||
Polymorphic ancestors must be defined on settings like this: | ||
|
@@ -436,6 +438,40 @@ JSON_API_POLYMORPHIC_ANCESTORS = ( | |
) | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could put a new paragraph here that includes comments about polymorphic libraries (and remove the "as backed by" parenthetical in the previous paragraph). How does the following sound?
|
||
#### Writing polymorphic resources | ||
|
||
A polymorphic endpoint can be setup if associated with a polymorphic serializer. | ||
|
||
A polymorphic serializer take care of (de)serializing the correct instances types and can be defined like this: | ||
|
||
|
||
```python | ||
class ProjectSerializer(serializers.PolymorphicModelSerializer): | ||
polymorphic_serializers = [ArtProjectSerializer, ResearchProjectSerializer] | ||
|
||
class Meta: | ||
model = models.Project | ||
``` | ||
|
||
It must inherit from `serializers.PolymorphicModelSerializer` and define the `polymorphic_serializers` list. | ||
This attribute defines the accepted resource types. | ||
|
||
|
||
Polymorphic relations can also be handled with `relations.PolymorphicResourceRelatedField` like this: | ||
|
||
```python | ||
class CompanySerializer(serializers.ModelSerializer): | ||
current_project = relations.PolymorphicResourceRelatedField( | ||
ProjectSerializer, queryset=models.Project.objects.all()) | ||
future_projects = relations.PolymorphicResourceRelatedField( | ||
ProjectSerializer, queryset=models.Project.objects.all(), many=True) | ||
|
||
class Meta: | ||
model = models.Company | ||
``` | ||
|
||
They must be explicitely declared with the `polymorphic_serializer` (first positional argument) correctly defined. | ||
|
||
It must be a subclass of `serializers.PolymorphicModelSerializer`. | ||
|
||
|
||
### Meta | ||
|
||
You may add metadata to the rendered json in two different ways: `meta_fields` and `get_root_meta`. | ||
|