Django Rest Swagger APIView

后端 未结 2 1380
臣服心动
臣服心动 2020-12-30 09:20

I made an API and want to make swagger doc. I don\'t develop any Serializes for that.

Views.py

class DeliveryView(APIVie         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 09:43

    The get_swagger_view() method does not give you the control to add parameters and descriptions to the urls in your app

    The solution for this is to use explicit schema definition. You can create a Document which is a representation of the Core API schema container. Go through the following link. Read the Core API section

    Core API schema generator

    This will require you to create a schema for the urls and the parameters that are used in your Application.

    Create a file for swagger

    swagger.py

    from rest_framework.decorators import renderer_classes, api_view
    from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer
    import coreapi
    from rest_framework import response
    # noinspection PyArgumentList
    @api_view()
    @renderer_classes([SwaggerUIRenderer, OpenAPIRenderer])
    def schema_view(request):
        print("---inside schema view-----")
        # noinspection PyArgumentList
        schema = coreapi.Document(
        title='Your Title',
        url='Your host url',
        content={
            'search': coreapi.Link(
                url='/search/',
                action='get',
                fields=[
                    coreapi.Field(
                        name='from',
                        required=True,
                        location='query',
                        description='City name or airport code.'
                    ),
                    coreapi.Field(
                        name='to',
                        required=True,
                        location='query',
                        description='City name or airport code.'
                    ),
                    coreapi.Field(
                        name='date',
                        required=True,
                        location='query',
                        description='Flight date in "YYYY-MM-DD" format.'
                    )
                ],
                description='Return flight availability and prices.'
            )
        }
    )
        # schema = generator.get_schema(request)
        return response.Response(schema)
    

    For explanation on coreapi.Document, coreapi.Field, coreapi.Link kindly refer to the above mentioned link.

    Create the url for the swagger docs:

    urls.py

    url('^docs', swagger.schema_view)
    

提交回复
热议问题