如何在没有serializer_class的基于类的视图中使用Swagger_Auto_Schema以及如何添加自定义身份验证权限?

发布于 2025-01-24 19:53:36 字数 724 浏览 0 评论 0原文

我有一个基于类的视图为:

class ClassBasedView(GenericAPIView):
    
    @swagger_auto_schema(responses={201: 'Created'})
    @authorize(myCustomPermission)
    def post(self, *args, **kwargs) -> Response:
        // code.....
        return Response(status=HTTPStatus.CREATED)

首先:

使用Swagger_Auto_Schema而没有任何序列化器的错误正在抛出错误。 assertionError:lidetview应该包括serializer_class属性,或覆盖get_serializer_class()方法。

,我不想在此端点上使用serialializer,因为我不需要。 但是Swagger_Auto_Schema一直在丢弃此错误。 我想知道是否有任何方法可以避免使用序列化器并获取此端点的招摇文档。

第二:

我想在文档中添加此端点的自定义授权许可。 Swagger_Auto_Schema中有一个安全字段,但不知道如何将其用于我的自定义许可类别IE mycustompermission

谢谢。

I have a class based view as:

class ClassBasedView(GenericAPIView):
    
    @swagger_auto_schema(responses={201: 'Created'})
    @authorize(myCustomPermission)
    def post(self, *args, **kwargs) -> Response:
        // code.....
        return Response(status=HTTPStatus.CREATED)

First:

The use of swagger_auto_schema without any serializer is throwing error as:
AssertionError: DetailView should either include a serializer_class attribute, or override the get_serializer_class() method.

And I don't want to use serializer for this endpoint as I don't need that.
But the swagger_auto_schema keeps on throwing this error.
I want to know whether there is any way to avoid the use of serializer and get the swagger documentation of this endpoint.

Second:

I want to add my custom authorisation permission of this endpoint in the doc.
There is a security field in swagger_auto_schema, but don't know how to make it to use for my custom permission class ie myCustomPermission

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2025-01-31 19:53:36
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

ClassBasedView_request_body = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'domain': openapi.Schema(type=openapi.TYPE_STRING),
        'subdomain': openapi.Schema(type=openapi.TYPE_STRING),
        'code': openapi.Schema(type=openapi.TYPE_INTEGER),
        'status': openapi.Schema(type=openapi.TYPE_BOOLEAN)
    },
    required=['domain', 'subdomain', 'code']
) 
ClassBasedView_200response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN),
        'Message': openapi.Schema(type=openapi.TYPE_STRING)  
    }
)    
ClassBasedView_400response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
        'Error': openapi.Schema(type=openapi.TYPE_STRING)
    },
)    
ClassBasedView_response = {
    '200': ClassBasedView_200response,
    '400': ClassBasedView_400response,
}

class ClassBasedView(GenericAPIView):

    @swagger_auto_schema(request_body=ClassBasedView_request_body, responses=ClassBasedView_response)
    def post(self, request, *args, **kwargs):
        try:
            pass
            #your code....
        except Exception as err:
            return Response({'Success': False, 'Error': err}, status=400)

对于您的第一个问题,此代码可能会对您有所帮助。

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

ClassBasedView_request_body = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'domain': openapi.Schema(type=openapi.TYPE_STRING),
        'subdomain': openapi.Schema(type=openapi.TYPE_STRING),
        'code': openapi.Schema(type=openapi.TYPE_INTEGER),
        'status': openapi.Schema(type=openapi.TYPE_BOOLEAN)
    },
    required=['domain', 'subdomain', 'code']
) 
ClassBasedView_200response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN),
        'Message': openapi.Schema(type=openapi.TYPE_STRING)  
    }
)    
ClassBasedView_400response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
        'Error': openapi.Schema(type=openapi.TYPE_STRING)
    },
)    
ClassBasedView_response = {
    '200': ClassBasedView_200response,
    '400': ClassBasedView_400response,
}

class ClassBasedView(GenericAPIView):

    @swagger_auto_schema(request_body=ClassBasedView_request_body, responses=ClassBasedView_response)
    def post(self, request, *args, **kwargs):
        try:
            pass
            #your code....
        except Exception as err:
            return Response({'Success': False, 'Error': err}, status=400)

For your first question this code might help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文