简体   繁体   English

Django 2.x drf-yasg如何在自定义方法中创建API(如在swagger中)

[英]Django 2.x drf-yasg how to create API in a custom method (like in swagger)

I am migrating my Django 1.11.7 to 2.x. 我正在将Django 1.11.7迁移到2.x。 One of the problems is django-rest-swagger, it is now deprecated. 问题之一是django-rest-swagger,现在已弃用。 And drf-yasg should now be the way for the API documentation and creation. 而且drf-yasg现在应该成为API文档和创建的方式。 I need to do it in a similar way of creating custom api as so it doesnt break anything in the mobile. 我需要以类似的方式来创建自定义api,因为它不会破坏移动设备中的任何内容。

Previously, it looks like this (django-rest-swagger==2.1.1) 以前看起来像这样(django-rest-swagger == 2.1.1)

旧版过时的招摇工具2.1.1 here is the old code snippet that works nicely in Django 1.11.7 and django-rest-swagger==2.1.1: 这是在Django 1.11.7和django-rest-swagger == 2.1.1中很好用的旧代码片段:

using swagger_schema.py
https://gist.github.com/axilaris/2f72fef8f30c56d5befe9e31cd76eb50


in url.py:

from rest_framework_swagger.views import get_swagger_view
from myapp.swagger_schema import SwaggerSchemaView


urlpatterns = [  
   url(r'^swaggerdoc/', SwaggerSchemaView.as_view()),
   url(r'^api/v1/general/get_countries$', api.get_countries, name='get_countries'),


in api.py:
@api_view(['POST'])
def get_countries(request):
    # ----- YAML below for Swagger -----
    """
    description: countries list
    parameters:
      - name: token
        type: string
        required: true
        location: form   
    """
    ......
    return Response(countries_list, status=status.HTTP_200_OK)

My question is how to do it in drf-yasg similarly, as I want to migrate this code and dont break anything on the mobile. 我的问题是类似地如何在drf-yasg中执行此操作,因为我想迁移此代码并且不破坏移动设备上的任何内容。

probably try to do this on this latest stable releases: 可能会尝试在此最新的稳定版本中执行此操作:

djangorestframework==3.9
drf-yasg==1.16.1

You can do it like this in your api.py . 您可以在api.py这样做。 This will generate exactly what you show in the screenshot: 这将完全生成您在屏幕截图中显示的内容:

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

from rest_framework.decorators import api_view, parser_classes
from rest_framework.parsers import FormParser

token = openapi.Parameter('token', openapi.IN_FORM, type=openapi.TYPE_STRING, required=True)


@swagger_auto_schema(
    method="post",
    manual_parameters=[token]
)
@api_view(["POST"])
@parser_classes([FormParser])
def get_countries(request):
    """
    Countries list
    """
    ......
    return Response(countries_list, status=status.HTTP_200_OK)

Note that I added @parser_classes([FormParser]) decorator to make sure the view accepts form data. 请注意,我添加了@parser_classes([FormParser])装饰器,以确保视图接受表单数据。 You can remove it if all your endpoints work only with form data and you set it globally in DRF settings . 如果所有端点仅适用于表单数据,并且已在DRF设置中对其进行全局设置 ,则可以将其删除。

Result: 结果: 结果

Here's more documentation on @swagger_auto_schema decorator . 这是有关@swagger_auto_schema装饰器的更多文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM