Django Rest Framework: How to enable swagger docs for function based views

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])
def app_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/


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

class TestView(View):
    @api_view(['GET', 'POST'])
    def get(self, request):
        ....

or

from django.utils.decorators import method_decorator
class TestView(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


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

    def get(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.


Add the following in your views.py

Imports

from rest_framework.schemas import AutoSchema
from rest_framework.compat import coreapi
#creating custom class 
class CustomSampleSchema(AutoSchema):
    def __init__(self):
        super(CustomSampleSchema, self).__init__()

    def get_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"}