Axi*_*xil 1 django swagger django-rest-framework drf-yasg redoc
我正在从 Django 1.11 --> 3.1.5 进行一些迁移工作
之前使用“rest_framework_swagger”,我可以通过 url.py 中的这个来完成 swagger api 分组
url(r'^api/v9/test_token1$',
api.test_token,
name='test_token'),
url(r'^api/v9/test_token2$',
api.test_token,
name='test_token'),
Run Code Online (Sandbox Code Playgroud)
并得到这个(注意它是 v9 组)
但是,我在 Django 3.1.5 url.py 上尝试过使用“drf_yasg”
path('/v2/token_api1', token_api1, name='token_api1'),
path('/v2/token_api2', token_api2, name='token_api2'),
Run Code Online (Sandbox Code Playgroud)
我的 api 定义(请注意我正在使用@api_view)
token = openapi.Parameter('token', openapi.IN_FORM, type=openapi.TYPE_STRING, required=True)
@swagger_auto_schema(
method="post",
manual_parameters=[token],
operation_id="token_api1"
)
@api_view(['POST'])
# this is optional and insures that the view gets formdata
@parser_classes([FormParser])
def token_api1(request):
token = request.POST['token']
return Response("success test_api:" + token, status=status.HTTP_200_OK)
token = openapi.Parameter('token', openapi.IN_FORM, type=openapi.TYPE_STRING, required=True)
@swagger_auto_schema(
method="post",
manual_parameters=[token],
operation_id="token_api2"
)
@api_view(['POST'])
# this is optional and insures that the view gets formdata
@parser_classes([FormParser])
def token_api2(request):
token = request.POST['token']
return Response("success test_api:" + token, status=status.HTTP_200_OK)
Run Code Online (Sandbox Code Playgroud)
但是,我明白了(注意到 v2 没有分组)。而且当我进行测试时,也出现了错误。(代码 404 错误:未找到)
如何将这些分组到 drf_yasg 中的 API 并确保没有错误?注意如果url.py是这样的,没有错误但它没有分组
path('token_api1', token_api1, name='token_api1'),
path('token_api2', token_api2, name='token_api2'),
Run Code Online (Sandbox Code Playgroud)
用于name从 Django / Python 代码访问端点。所以我相信较新版本的 Django 禁止重复名称。
您可以通过在 下提供相同的标签来对端点进行分组tags。像这样:
@swagger_auto_schema(tags=["my_custom_tag"], auto_schema=NoPagingAutoSchema, filter_inspectors=[DjangoFilterDescriptionInspector])
@action(detail=False, methods=['get'])
def action1(self, request):
pass
@swagger_auto_schema(tags=["my_custom_tag"], method='delete', manual_parameters=[openapi.Parameter(
name='delete_form_param', in_=openapi.IN_FORM,
type=openapi.TYPE_INTEGER,
description="this should not crash (form parameter on DELETE method)"
)])
@action(detail=True, methods=['delete'])
def action2(self, request, slug=None):
pass
Run Code Online (Sandbox Code Playgroud)
请注意,您还可以为每个功能提供多个标签,因此它们将显示在多个不同的类别(或组)中。
结果:
| 归档时间: |
|
| 查看次数: |
3652 次 |
| 最近记录: |