18

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 function based views as shown below:

@api_view(['GET', 'POST'])
def app_info(request): 
    ...
    return response

Most of my views.py is filled with function based views, just like above.

Any help on how to enable the same will greatly appreciated. Thanks!

I am using Django: 1.8; Django REST Swagger: 2.1.2; DRF: 3.6.2

Abijith Mg
  • 2,647
  • 21
  • 35

3 Answers3

17

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/

lapinkoira
  • 8,320
  • 9
  • 51
  • 94
Igonato
  • 10,175
  • 3
  • 35
  • 64
3

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.

chujie.ao
  • 41
  • 4
2

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"}
Community
  • 1
  • 1
Nithin
  • 31
  • 1