Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7809f75

Browse files
leo-naekaograycode
authored andcommittedMay 13, 2016
Polymorphic ancestors must now be defined in Django's settings
Update documentation
1 parent 6110a9c commit 7809f75

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed
 

‎docs/usage.md

Lines changed: 13 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@
2626
HyperlinkedRouterField = type(None)
2727

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

4033

4134
def get_resource_name(context):

0 commit comments

Comments
 (0)
Please sign in to comment.