DRF 接口文档添加请求参数描述

DRF 接口文档添加请求参数描述

安装django-rest-framework,编写视图函数之后,发现生成的接口文档没有请求参数,如下

解决方案

from rest_framework.views import Response, APIView
import coreschema  # 导入
from rest_framework.schemas import ManualSchema  # 导入
import coreapi  # 导入
class User(APIView):
    # 编写以下内容
    schema = ManualSchema(fields=[    
        coreapi.Field(
            "first_field",
            required=True,
            location="path",
            schema=coreschema.String(),
            description="hello world"
        ),
        coreapi.Field(
            "second_field",
            required=False,
            location="path",
            schema=coreschema.String(),
            description=" name"
        ),
    ])
    def get(self, request):
        """
            get:
           获取用户信息.
        """
        return Response({"status": True, "method": request.method, "data": ""})

结果

新的问题

原本的接口描述不见了,先这样,后续解决

原文地址:https://www.cnblogs.com/jruing/p/14658157.html