Skip to content Skip to sidebar Skip to footer

Django Rest Framework: How To Enable Swagger Docs For Function Based Views

I went through Django REST Swagger 2.1.2 documentation. When I tried with class based views, it was working fine. But i did not find any reference on how to enable swagger for func

Solution 1:

You should be able to use @renderer_classes decorator:

from rest_framework_swagger import renderers
from rest_framework.decorators import api_view, renderer_classes


@api_view(['GET', 'POST'])@renderer_classes([renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer])defapp_info(request): 
    ...
    return response

Also, it should be worth mentioning, that if you don't want to use this decorator on every view you can specify DEFAULT_RENDERER_CLASSES in settings

EDIT: It seems it's in the docs after all. Check the very bottom of this page: https://django-rest-swagger.readthedocs.io/en/latest/schema/

Solution 2:

i am not fammiliar with swagger,but you may try to use the decorator in this way:

classTestView(View):
    @api_view(['GET', 'POST'])defget(self, request):
        ....

or

from django.utils.decorators import method_decorator
classTestView(View):
    @method_decorator(api_view(['GET', 'POST'])
    def get(self, request):
        ....

----------------------------------------------------------------------------

sorry, maybe i have misunderstood your question. according to the document, if you want to enable swagger in class based view. there is example:

from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView
from rest_framework_swagger import renderers


classSwaggerSchemaView(APIView):
    permission_classes = [AllowAny]
    renderer_classes = [
        renderers.OpenAPIRenderer,
        renderers.SwaggerUIRenderer
    ]

    defget(self, request):
        generator = SchemaGenerator()
        schema = generator.get_schema(request=request)
        return Response(schema)

restframework will use these two renderer_classes to render Json and UI.

Solution 3:

Add the following in your views.py

Imports

from rest_framework.schemasimportAutoSchemafrom rest_framework.compatimport coreapi
#creating custom class classCustomSampleSchema(AutoSchema):
    def__init__(self):
        super(CustomSampleSchema, self).__init__()

    defget_manual_fields(self, path, method):
        extra_fields = [
            coreapi.Field('field1', required=True, location='form', description='', type='', example=''),
            coreapi.Field('field2', required=False, location='form', description='', type='', example=''),
            coreapi.Field('field3', required=False, location='form', description='', type='', example='')

        ]
        manual_fields = super().get_manual_fields(path, method)
        return manual_fields + extra_fields

This is the function you're writing swagger doc for.

@api_view(['post'])
@schema(CustomSampleSchema())
@csrf_exempt
def func_name(request, param):
"""
Your function definition below
"""

Sample json input

{"name":"['name1', ]","places":"['place1', 'place2']","key":"12345"}

Post a Comment for "Django Rest Framework: How To Enable Swagger Docs For Function Based Views"