| 1 |
.. _django-rest-framework-support: |
| 2 |
|
| 3 |
=================== |
| 4 |
djangorestframework |
| 5 |
=================== |
| 6 |
|
| 7 |
Polymorphic serializers for `Django REST Framework <https://www.django-rest-framework.org/>`_. |
| 8 |
|
| 9 |
The :pypi:`django-rest-polymorphic` package has been incorporated into :pypi:`django-polymorphic`. |
| 10 |
This contrib package allows you to easily define serializers for your inherited models that you have |
| 11 |
created using ``django-polymorphic`` library. To migrate from :pypi:`django-rest-polymorphic`, you |
| 12 |
need to change your import paths from ``rest_polymorphic.serializers`` to |
| 13 |
``polymorphic.contrib.drf.serializers``. |
| 14 |
|
| 15 |
Usage |
| 16 |
----- |
| 17 |
|
| 18 |
Define your polymorphic models: |
| 19 |
|
| 20 |
.. literalinclude:: ../../src/polymorphic/tests/examples/integrations/drf/models/example_models.py |
| 21 |
:language: python |
| 22 |
:linenos: |
| 23 |
|
| 24 |
|
| 25 |
Define serializers for each polymorphic model the way you did it when you used |
| 26 |
:pypi:`djangorestframework`: |
| 27 |
|
| 28 |
.. literalinclude:: ../../src/polymorphic/tests/examples/integrations/drf/example_serializers.py |
| 29 |
:language: python |
| 30 |
:linenos: |
| 31 |
:lines: 1-29 |
| 32 |
|
| 33 |
|
| 34 |
Note that if you extend `HyperlinkedModelSerializer |
| 35 |
<https://www.django-rest-framework.org/api-guide/serializers/#hyperlinkedmodelserializer>`_ instead |
| 36 |
of `ModelSerializer <https://www.django-rest-framework.org/api-guide/serializers/#modelserializer>`_ |
| 37 |
you need to define `extra_kwargs |
| 38 |
<https://www.django-rest-framework.org/community/3.0-announcement/#the-extra_kwargs-option>`_ to |
| 39 |
direct the URL to the appropriate view for your polymorphic serializer. |
| 40 |
|
| 41 |
Then you have to create a polymorphic serializer that serves as a mapper between models and |
| 42 |
serializers which you have defined above: |
| 43 |
|
| 44 |
.. literalinclude:: ../../src/polymorphic/tests/examples/integrations/drf/example_serializers.py |
| 45 |
:language: python |
| 46 |
:lines: 32- |
| 47 |
|
| 48 |
Create viewset with serializer_class equals to your polymorphic serializer: |
| 49 |
|
| 50 |
.. literalinclude:: ../../src/polymorphic/tests/examples/integrations/drf/views.py |
| 51 |
:language: python |
| 52 |
:linenos: |
| 53 |
|
| 54 |
Test it: |
| 55 |
|
| 56 |
.. code-block:: bash |
| 57 |
|
| 58 |
$ http GET "http://localhost:8000/projects/" |
| 59 |
|
| 60 |
.. code-block:: http |
| 61 |
|
| 62 |
HTTP/1.0 200 OK |
| 63 |
Content-Length: 227 |
| 64 |
Content-Type: application/json |
| 65 |
|
| 66 |
[ |
| 67 |
{ |
| 68 |
"resourcetype": "Project", |
| 69 |
"topic": "John's gathering" |
| 70 |
}, |
| 71 |
{ |
| 72 |
"artist": "T. Turner", |
| 73 |
"resourcetype": "ArtProject", |
| 74 |
"topic": "Sculpting with Tim", |
| 75 |
"url": "http://localhost:8000/projects/2/" |
| 76 |
}, |
| 77 |
{ |
| 78 |
"resourcetype": "ResearchProject", |
| 79 |
"supervisor": "Dr. Winter", |
| 80 |
"topic": "Swallow Aerodynamics" |
| 81 |
} |
| 82 |
] |
| 83 |
|
| 84 |
.. code-block:: bash |
| 85 |
|
| 86 |
$ http POST "http://localhost:8000/projects/" resourcetype="ArtProject" topic="Guernica" artist="Picasso" |
| 87 |
|
| 88 |
.. code-block:: http |
| 89 |
|
| 90 |
HTTP/1.0 201 Created |
| 91 |
Content-Length: 67 |
| 92 |
Content-Type: application/json |
| 93 |
|
| 94 |
{ |
| 95 |
"artist": "Picasso", |
| 96 |
"resourcetype": "ArtProject", |
| 97 |
"topic": "Guernica", |
| 98 |
"url": "http://localhost:8000/projects/4/" |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
Customize resource type |
| 103 |
----------------------- |
| 104 |
|
| 105 |
As you can see from the example above, in order to specify the type of your polymorphic model, you |
| 106 |
need to send a request with resource type field. The value of resource type should be the name of |
| 107 |
the model. |
| 108 |
|
| 109 |
If you want to change the resource type field name from ``resourcetype`` to something else, you |
| 110 |
should override ``resource_type_field_name`` attribute: |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
class ProjectPolymorphicSerializer(PolymorphicSerializer): |
| 115 |
resource_type_field_name = 'projecttype' |
| 116 |
... |
| 117 |
|
| 118 |
If you want to change the behavior of resource type, you should override ``to_resource_type`` |
| 119 |
method: |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
class ProjectPolymorphicSerializer(PolymorphicSerializer): |
| 124 |
... |
| 125 |
|
| 126 |
def to_resource_type(self, model_or_instance): |
| 127 |
return model_or_instance._meta.object_name.lower() |
| 128 |
|
| 129 |
Now, the request for creating new object will look like this: |
| 130 |
|
| 131 |
.. code-block:: bash |
| 132 |
|
| 133 |
$ http POST "http://localhost:8000/projects/" projecttype="artproject" topic="Guernica" artist="Picasso" |