Add support for SerializerMethodField (#179)

Closes #137, #179.
This commit is contained in:
John Carter
2018-08-08 06:23:36 +12:00
committed by Cristi Vîjdea
parent 1dd7cfe043
commit 748b5d3c2f
12 changed files with 507 additions and 4 deletions
+14 -1
View File
@@ -4,9 +4,22 @@ Changelog
**********
**1.9.2**
**1.10.0**
**********
*Release date: TBD, 2018*
- **IMPROVED:** added support for ``SerializerMethodField``, via the ``swagger_serializer_method`` decorator for the
method field, and support for Python 3.5 style type hinting of the method field return type
(:issue:`137`, :pr:`175`, :pr:`179`)
*NOTE:* in order for this to work, you will have to add the new ``drf_yasg.inspectors.SerializerMethodFieldInspector``
to your ``DEFAULT_FIELD_INSPECTORS`` array if you changed it from the default value
*********
**1.9.2**
*********
*Release date: Aug 03, 2018*
- **IMPROVED:** updated ``swagger-ui`` to version 3.17.6
+46
View File
@@ -155,6 +155,49 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
replacing/decorating methods on the base class itself.
*********************************
Support for SerializerMethodField
*********************************
Schema generation of ``serializers.SerializerMethodField`` supported in two ways:
1) The decorator ``swagger_serializer_method(serializer)`` for the use case where the serializer method
is using a serializer. e.g.:
.. code-block:: python
from drf_yasg.utils import swagger_serializer_method
class OtherStuffSerializer(serializers.Serializer):
foo = serializers.CharField()
class ParentSerializer(serializers.Serializer):
other_stuff = serializers.SerializerMethodField()
@swagger_serializer_method(serializer=OtherStuffSerializer)
def get_other_stuff(self, obj):
return OtherStuffSerializer().data
Note that the serializer parameter can be either be a serializer class or instance
2) For simple cases where the method is returning one of the supported types,
`Python 3 type hinting`_ of the serializer method return value can be used. e.g.:
.. code-block:: python
class SomeSerializer(serializers.Serializer):
some_number = serializers.SerializerMethodField()
def get_some_number(self, obj) -> float:
return 1.0
********************************
Serializer ``Meta`` nested class
********************************
@@ -333,3 +376,6 @@ A second example, of a :class:`~.inspectors.FieldInspector` that removes the ``t
Another caveat that stems from this is that any serializer named "``NestedSerializer``" will be forced inline
unless it has a ``ref_name`` set explicitly.
.. _Python 3 type hinting: https://docs.python.org/3/library/typing.html
+1
View File
@@ -76,6 +76,7 @@ to this list.
:class:`'drf_yasg.inspectors.DictFieldInspector' <.inspectors.DictFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.HiddenFieldInspector' <.inspectors.HiddenFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.RecursiveFieldInspector' <.inspectors.RecursiveFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.SerializerMethodFieldInspector' <.inspectors.SerializerMethodFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.SimpleFieldInspector' <.inspectors.SimpleFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.StringDefaultFieldInspector' <.inspectors.StringDefaultFieldInspector>`, |br| \
``]``