英文:
drf-spectacular error - Function Based Views - unable to guess serializer
问题
我在尝试生成模式时遇到以下错误:
"错误[get_token]:无法猜测序列化程序。这是针对API视图的宽松回退处理。如果视图在您的控制之下,请考虑使用GenericAPIView作为视图基类。无论如何,您可能希望添加一个serializer_class(或方法)"
我正在使用基于函数的视图,如下所示:
from .serializers.auth import GetTokenSerializer
@api_view(["POST"])
@authentication_classes([UserTypeBasicAuthentication])
@permission_classes([IsAuthenticated])
@check_request(GetTokenSerializer)
@extend_schema(
request=GetTokenSerializer,
responses=None
)
def get_token(request: Request) -> Response:
...
我不明白我做错了什么。任何帮助将不胜感激。谢谢!
英文:
I'm getting the following error when trying to generate the schema:
" Error [get_token]: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method)"
I'm using a function based view, snippet below:
from .serializers.auth import GetTokenSerializer
@api_view(["POST"])
@authentication_classes([UserTypeBasicAuthentication])
@permission_classes([IsAuthenticated])
@check_request(GetTokenSerializer)
@extend_schema(
request=GetTokenSerializer,
responses=None
)
def get_token(request: Request) -> Response:
...
I don't understand what I'm doing wrong. Any help would be appreciated. Thank you!
答案1
得分: 3
我找到了drf-spectacular的出色创作者的答案。解决方法在这里:
https://github.com/tfranzel/drf-spectacular/issues/969
基本上,你应该在顶部使用@extend_schema装饰器,特别是在@api_view上方。
英文:
Found an answer from the wonderful creator of drf-spectacular. Solve is here:
https://github.com/tfranzel/drf-spectacular/issues/969
Basically, you should put the @extend_schema decorator on top, especially over @api_view
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论