drf-spectacular错误 – 基于函数的视图 – 无法猜测序列化器

huangapple go评论67阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月31日 16:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定