安装依赖
djangorestframework
drf-yasg
settings.py
INSTALLED_APPS = [
...
'rest_framework',
'drf_yasg',
...
]
SWAGGER_SETTINGS = {
'LOGIN_URL': '/admin/login',
'LOGOUT_URL': '/admin/logout',
'PERSIST_AUTH': True,
'REFETCH_SCHEMA_WITH_AUTH': True,
'REFETCH_SCHEMA_ON_LOGOUT': True,
'DEFAULT_INFO': 'accentureTest.urls.swagger_info',
'SECURITY_DEFINITIONS': {
'Basic': {
'type': 'basic'
},
'Bearer': {
'type': 'apiKey',
'name': 'authorization',
'in': 'header'
},
'Query': {
'type': 'apiKey',
'name': 'auth',
'in': 'query'
}
}
}
urls.py
from django.contrib import admin
from django.urls import path, include
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
schema_view = get_schema_view(
openapi.Info(
title="测试工程API",
default_version='v1.0',
description="测试工程接口文档",
terms_of_service="https://www.cnblogs.com/jinjiangongzuoshi/",
contact=openapi.Contact(email="狂师"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
path('admin/', admin.site.urls),
path('tasks/', include("tasks.urls")),
path('api/', include('api.urls')),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('swagger', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
views.py
查询字符串
class RoleUserApiView(APIView):
"""角色用户管理"""
role_id = openapi.Parameter("role_id", openapi.IN_QUERY, description="角色id",
type=openapi.TYPE_INTEGER, required=True)
@swagger_auto_schema(
operation_description="查询xxx",
responses={200: "success"},
manual_parameters=[role_id]
)
def get(self, request):
role_id = request.query_params.get("role_id")
...
return Response({"status": "success"},status=status.HTTP_200_OK)
Body
class RoleUserApiView(APIView):
"""角色用户管理"""
@swagger_auto_schema(
operation_description="增加xxx",
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
required=["role_id", "user_ids"],
properties={
"role_id": openapi.Schema(type=openapi.TYPE_INTEGER, description="role_id"),
"user_ids": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="user_ids",
items=openapi.Items(type=openapi.TYPE_INTEGER),
),
},
),
)
def post(self, request):
"""一个角色对应多个用户"""
role_id = request.data.get("role_id")
user_ids = request.data.get("user_ids")
...
return Response({"status": "success"},status=status.HTTP_200_OK)
Form
from rest_framework.parsers import MultiPartParser
class UploadFileApiView(APIView):
parser_classes = (MultiPartParser,)
name = openapi.Parameter("name", openapi.IN_FORM, description="名字", type=openapi.TYPE_STRING, required=True)
age = openapi.Parameter("age", openapi.IN_FORM, description="年龄", type=openapi.TYPE_INTEGER, required=True)
file = openapi.Parameter("file", openapi.IN_FORM, description="文件名", type=openapi.TYPE_FILE, required=True)
@swagger_auto_schema(
operation_description="上传文件",
responses={200: "success"},
manual_parameters=[name, age, file],
)
def post(self, request):
name = request.data.get("name")
age = request.data.get("age")
file = request.data.get("file")
...
return Response("success")