在 DRF 中使用 @extend_schema 而不是 @actoin 的问题
Problem to use @extend_schema over an @actoin in DRF
你好我的代码中有一个@extend_schema of drf_spectacular库我需要使用它通过我的 @action 自定义 OpenAPI 中的详细信息,但我遇到了这样的错误
Internal Server Error: /api/schema/
Traceback (most recent call last):
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 482, in thread_handler
raise exc_info[1]
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 38, in inner
response = await get_response(request)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/core/handlers/base.py", line 233, in _get_response_async
response = await wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 444, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
return await fut
File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
return func(*args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/views.py", line 69, in get
return self._get_schema_response(request)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/views.py", line 77, in _get_schema_response
data=generator.get_schema(request=request, public=self.serve_public),
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/generators.py", line 262, in get_schema
paths=self.parse(request, public),
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/generators.py", line 227, in parse
assert isinstance(view.schema, AutoSchema), (
AssertionError: Incompatible AutoSchema used on View <class 'WEB_SERVER.api_v1.views.Device'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?
HTTP GET /api/schema/ 500 [0.10, 127.0.0.1:51558]
这是我的代码:
@extend_schema(summary="short summary")
@action(detail=True, methods=["GET"], url_name="command")
def command(self, request, pk) -> Union[Response, Http404]: # Retrieve Command
data = request.data
status = get_object_or_404(Command_Model, device=pk)
serializer = Serializer_Command(status)
return Response(serializer.data, status=Status.HTTP_200_OK)
@command.mapping.post
def command_create(self, request, pk) -> Response:
data = request.data.copy()
data["device"] = pk
command_instace = None
try:
command_instace = Command_Model.objects.get(device=pk)
except:
pass
serializer = Serializer_Command(
instance=command_instace, data=data, partial=True
)
if serializer.is_valid(raise_exception_validitor):
serializer.save()
return Response(data=serializer.data, status=Status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)
我尝试将装饰器移动到@action 的下方,但不起作用@extend_schema 实际上我没有收到任何错误,但在 OpenAPI 中它不起作用
同样,当我从我的代码中删除 @command.mapping.post 时,它的工作非常好
我通过添加更多 @extend_schema 到其他 解决了这个问题额外的端点,如果没有,你会得到像我这样的错误
@extend_schema(summary="short summary")
@action(detail=True, methods=["GET"], url_name="command")
def command(self, request, pk) -> Union[Response, Http404]: # Retrieve Command
data = request.data
status = get_object_or_404(Command_Model, device=pk)
serializer = Serializer_Command(status)
return Response(serializer.data, status=Status.HTTP_200_OK)
@extend_schema(summary="short summary")
@command.mapping.post
def command_create(self, request, pk) -> Response:
data = request.data.copy()
data["device"] = pk
command_instace = None
try:...
你好我的代码中有一个@extend_schema of drf_spectacular库我需要使用它通过我的 @action 自定义 OpenAPI 中的详细信息,但我遇到了这样的错误
Internal Server Error: /api/schema/
Traceback (most recent call last):
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 482, in thread_handler
raise exc_info[1]
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 38, in inner
response = await get_response(request)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/core/handlers/base.py", line 233, in _get_response_async
response = await wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 444, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
return await fut
File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
return func(*args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/views.py", line 69, in get
return self._get_schema_response(request)
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/views.py", line 77, in _get_schema_response
data=generator.get_schema(request=request, public=self.serve_public),
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/generators.py", line 262, in get_schema
paths=self.parse(request, public),
File "/mnt/62EE2B18EE2AE44F/NEW/django/webserver/django-env/lib/python3.9/site-packages/drf_spectacular/generators.py", line 227, in parse
assert isinstance(view.schema, AutoSchema), (
AssertionError: Incompatible AutoSchema used on View <class 'WEB_SERVER.api_v1.views.Device'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?
HTTP GET /api/schema/ 500 [0.10, 127.0.0.1:51558]
这是我的代码:
@extend_schema(summary="short summary")
@action(detail=True, methods=["GET"], url_name="command")
def command(self, request, pk) -> Union[Response, Http404]: # Retrieve Command
data = request.data
status = get_object_or_404(Command_Model, device=pk)
serializer = Serializer_Command(status)
return Response(serializer.data, status=Status.HTTP_200_OK)
@command.mapping.post
def command_create(self, request, pk) -> Response:
data = request.data.copy()
data["device"] = pk
command_instace = None
try:
command_instace = Command_Model.objects.get(device=pk)
except:
pass
serializer = Serializer_Command(
instance=command_instace, data=data, partial=True
)
if serializer.is_valid(raise_exception_validitor):
serializer.save()
return Response(data=serializer.data, status=Status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)
我尝试将装饰器移动到@action 的下方,但不起作用@extend_schema 实际上我没有收到任何错误,但在 OpenAPI 中它不起作用
同样,当我从我的代码中删除 @command.mapping.post 时,它的工作非常好
我通过添加更多 @extend_schema 到其他 解决了这个问题额外的端点,如果没有,你会得到像我这样的错误
@extend_schema(summary="short summary")
@action(detail=True, methods=["GET"], url_name="command")
def command(self, request, pk) -> Union[Response, Http404]: # Retrieve Command
data = request.data
status = get_object_or_404(Command_Model, device=pk)
serializer = Serializer_Command(status)
return Response(serializer.data, status=Status.HTTP_200_OK)
@extend_schema(summary="short summary")
@command.mapping.post
def command_create(self, request, pk) -> Response:
data = request.data.copy()
data["device"] = pk
command_instace = None
try:...