Django Rest Swagger APIView

2020-02-26 00:11发布

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

Views.py

class DeliveryView(APIView):

    renderer_classes = (XMLRenderer,)

    def get_campaign_vast(self, request, *args):
        return response

    def get(self, request):
        return self.get_campaign_vast(request, data)

    def post(self, request):
        """ 
        This text is the description for this API
        ---
        param1 -- A first parameter
        param2 -- A second parameter
        """
        data = request.data
        return self.get_campaign_vast(request, data) 

urls.py

from django.conf.urls import url,include
from django.contrib import admin
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Add Delivery')

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',schema_view),
    url(r'^', include('deliverymanagment.urls')),
]

I want to get all the parameters in Swagger which i am not getting.

i am not able to get parameters

i am using:

django-rest-swagger==2.1.1

djangorestframework==3.5.3

2条回答
Evening l夕情丶
2楼-- · 2020-02-26 00:24

Consider using the GenericAPIView instead as this will generate the documentation. It is a bit of a hack using this when the endpoint does not relate to a model, but it does work.

As an example, the following code will create an endpoint that only accepts post requests and is documented within swagger using the seralizer.

class SomeThing(GenericAPIView):
    serializer_class = MySerializer

    def post(self, request, *args, **kwargs):
        serializer = MySerializer(data=request.data)
        if serializer.is_valid() is False:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        res = do_magic(**serializer.data)
        return Response(res)
查看更多
Luminary・发光体
3楼-- · 2020-02-26 00:25

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)
查看更多
登录 后发表回答