Skip to content

Commit 65dce93

Browse files
committed
Polymorphic ancestors must now be defined in Django's settings
Update documentation
1 parent 1ad3f65 commit 65dce93

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

docs/usage.md

+13
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ When set to pluralize:
248248
Both `JSON_API_PLURALIZE_RELATION_TYPE` and `JSON_API_FORMAT_RELATION_KEYS` can be combined to
249249
achieve different results.
250250

251+
### Working with polymorphic resources
252+
253+
This package can defer the resolution of the type of polymorphic models instances to get the appropriate type.
254+
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.
255+
256+
Polymorphic ancestors must be defined on settings like this:
257+
258+
```python
259+
JSON_API_POLYMORPHIC_ANCESTORS= (
260+
'polymorphic.models.PolymorphicModel',
261+
)
262+
```
263+
251264
### Meta
252265

253266
You may add metadata to the rendered json in two different ways: `meta_fields` and `get_root_meta`.

example/settings/test.py

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

rest_framework_json_api/utils.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@
2222
HyperlinkedRouterField=type(None)
2323

2424
POLYMORPHIC_ANCESTORS= ()
25-
try:
26-
frompolymorphic.modelsimportPolymorphicModel
27-
POLYMORPHIC_ANCESTORS+= (PolymorphicModel,)
28-
exceptImportError:
29-
pass
30-
try:
31-
fromtypedmodels.modelsimportTypedModel
32-
POLYMORPHIC_ANCESTORS+= (TypedModel,)
33-
exceptImportError:
34-
pass
25+
forancestoringetattr(settings, 'JSON_API_POLYMORPHIC_ANCESTORS', ()):
26+
ancestor_class=import_class_from_dotted_path(ancestor)
27+
POLYMORPHIC_ANCESTORS+= (ancestor_class,)
3528

3629

3730
defget_resource_name(context):

0 commit comments

Comments
 (0)
close