Add swagger_fake_view marker to help detect fake views in get_serializer
Cleaner fix for #154openapi3
parent
e0aec3ff45
commit
696ec3a94a
|
|
@ -174,6 +174,15 @@ in a few ways:
|
|||
:ref:`@swagger_auto_schema <custom-spec-swagger-auto-schema>`, to prevent ``get_serializer`` from being called on
|
||||
the view
|
||||
* :ref:`exclude your endpoint from introspection <custom-spec-excluding-endpoints>`
|
||||
* use the ``swagger_fake_view`` marker to detect requests generated by ``drf-yasg``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def get_serializer_class(self):
|
||||
if getattr(self, 'swagger_fake_view', False):
|
||||
return TodoTreeSerializer
|
||||
|
||||
raise NotImplementedError("must not call this")
|
||||
|
||||
.. _SCRIPT_NAME: https://www.python.org/dev/peps/pep-0333/#environ-variables
|
||||
.. _FORCE_SCRIPT_NAME: https://docs.djangoproject.com/en/2.0/ref/settings/#force-script-name
|
||||
|
|
|
|||
|
|
@ -243,6 +243,8 @@ class OpenAPISchemaGenerator(object):
|
|||
view_method = getattr(view, method, None)
|
||||
if view_method is not None: # pragma: no cover
|
||||
setattr(view_method.__func__, '_swagger_auto_schema', overrides)
|
||||
|
||||
setattr(view, 'swagger_fake_view', True)
|
||||
return view
|
||||
|
||||
def get_endpoints(self, request):
|
||||
|
|
|
|||
|
|
@ -33,7 +33,12 @@ class NestedTodoView(RetrieveAPIView):
|
|||
|
||||
class TodoTreeView(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = TodoTree.objects.all()
|
||||
serializer_class = TodoTreeSerializer
|
||||
|
||||
def get_serializer_class(self):
|
||||
if getattr(self, 'swagger_fake_view', False):
|
||||
return TodoTreeSerializer
|
||||
|
||||
raise NotImplementedError("must not call this")
|
||||
|
||||
|
||||
class TodoRecursiveView(viewsets.ModelViewSet):
|
||||
|
|
|
|||
Loading…
Reference in New Issue