Skip to content

Commit 8c73d95

Browse files
committed
Polymorphic ancestors must now be defined in Django's settings
Update documentation
1 parent dd4d4ec commit 8c73d95

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
@@ -423,6 +423,19 @@ field_name_mapping = {
423423
```
424424

425425

426+
### Working with polymorphic resources
427+
428+
This package can defer the resolution of the type of polymorphic models instances to get the appropriate type.
429+
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.
430+
431+
Polymorphic ancestors must be defined on settings like this:
432+
433+
```python
434+
JSON_API_POLYMORPHIC_ANCESTORS = (
435+
'polymorphic.models.PolymorphicModel',
436+
)
437+
```
438+
426439
### Meta
427440

428441
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
@@ -27,16 +27,9 @@
2727
HyperlinkedRouterField = type(None)
2828

2929
POLYMORPHIC_ANCESTORS = ()
30-
try:
31-
from polymorphic.models import PolymorphicModel
32-
POLYMORPHIC_ANCESTORS += (PolymorphicModel,)
33-
except ImportError:
34-
pass
35-
try:
36-
from typedmodels.models import TypedModel
37-
POLYMORPHIC_ANCESTORS += (TypedModel,)
38-
except ImportError:
39-
pass
30+
for ancestor in getattr(settings, 'JSON_API_POLYMORPHIC_ANCESTORS', ()):
31+
ancestor_class = import_class_from_dotted_path(ancestor)
32+
POLYMORPHIC_ANCESTORS += (ancestor_class,)
4033

4134

4235
def get_resource_name(context):

0 commit comments

Comments
 (0)