6
class Templates(APIView):
    parser_classes = (FormParser,)

    @swagger_auto_schema(manual_parameters=[
        openapi.Parameter('title', in_=openapi.IN_FORM, description='作品标题', type=openapi.TYPE_STRING, required=True)]
    )
    def post(self, reuqest):
        pass

当我指定in_=openapi.IN_FORM并且parser_classes = (FormParser,)swaager首页工作时,但如果我修改in_=openapi.IN_BODY,则会出现如下错误:

Traceback (most recent call last):
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\pythonenv\forecast_env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\views.py", line 94, in get
    schema = generator.get_schema(request, self.public)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 254, in get_schema
    paths, prefix = self.get_paths(endpoints, components, request, public)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 412, in get_paths
    operation = self.get_operation(view, path, prefix, method, components, request)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\generators.py", line 454, in get_operation
    operation = view_inspector.get_operation(operation_keys)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\inspectors\view.py", line 36, in get_operation
    parameters = self.add_manual_parameters(parameters)
  File "C:\pythonenv\forecast_env\lib\site-packages\drf_yasg\inspectors\view.py", line 162, in add_manual_parameters
    raise SwaggerGenerationError("specify the body parameter as a Schema or Serializer in request_body")
drf_yasg.errors.SwaggerGenerationError: specify the body parameter as a Schema or Serializer in request_body
[16/Jul/2020 16:39:48] "GET /api/swagger?format=openapi HTTP/1.1" 500 19229

有没有人可以给我一个关于在 drf-yasg 上设置应用程序/json 参数的示例?

drf-yasg  1.17.1
Django    3.0.5
4

1 回答 1

8

您应该使用 request_body 参数

    @swagger_auto_schema(methods=['post'],
                         request_body=openapi.Schema(
                             type=openapi.TYPE_OBJECT,
                             required=['version'],
                             properties={
                                 'version': openapi.Schema(type=openapi.TYPE_STRING)
                             },
                         ),
                         operation_description='Uninstall a version of Site')
    def post(self, request):
        pass
于 2020-12-04T12:33:15.413 回答