drf_yasg swagger_auto_schema从视图移动

发布于 2025-02-10 23:30:10 字数 1304 浏览 0 评论 0原文

在我的项目视图类中,我使用Swagger_Auto_Schema装饰器来自定义Swagger可能的响应。我的问题是:“这是将所有这些装饰符从我的基于我的班级视图转移到模块或python文件等其他地方的一种方式吗?”。我之所以问这个问题,是因为Swagger_Auto_Schema装饰器有时会占据我观点的大部分代码,而且很难阅读它们。

示例:

class ArticleListCreateAPIView(ListCreateAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def get_serializer_class(self):
        if self.request.method == "GET":
            return ArticleResponseSerializer
        return super().get_serializer_class()
    
    @swagger_auto_schema(
        responses={
            status.HTTP_201_CREATED: openapi.Response(
                description="Successful article create",
                schema=ArticleResponseSerializer,
            ),
            status.HTTP_400_BAD_REQUEST: openapi.Response(
                description="Data serialization failed. Incorrect data.",
                schema=DetailResponseSerializer
            ),
            status.HTTP_409_CONFLICT: openapi.Response(
                description="Media-file is already bouned to some other model",
                schema=DetailResponseSerializer
            )
        }
    )
    def post(self, request, *args, **kwargs):
        ...

也许我应该停止使用Swagger_Auto_Schema并用其他一些结构替换它?

In my project's views classes I use swagger_auto_schema decorator to customize swagger possible responses. My question is this: "Is this a way to move all these decorators from my class based views to other place like module or python file?". I'm asking this question because swagger_auto_schema decorators sometimes take up most of the code of my views, and it's getting hard to read them.

example:

class ArticleListCreateAPIView(ListCreateAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def get_serializer_class(self):
        if self.request.method == "GET":
            return ArticleResponseSerializer
        return super().get_serializer_class()
    
    @swagger_auto_schema(
        responses={
            status.HTTP_201_CREATED: openapi.Response(
                description="Successful article create",
                schema=ArticleResponseSerializer,
            ),
            status.HTTP_400_BAD_REQUEST: openapi.Response(
                description="Data serialization failed. Incorrect data.",
                schema=DetailResponseSerializer
            ),
            status.HTTP_409_CONFLICT: openapi.Response(
                description="Media-file is already bouned to some other model",
                schema=DetailResponseSerializer
            )
        }
    )
    def post(self, request, *args, **kwargs):
        ...

Maybe I should stop using swagger_auto_schema and replace it by some other structures?

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

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

发布评论

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

评论(1

沫离伤花 2025-02-17 23:30:10

我的解决方案是将响应零件,响应和Swagger_Auto_Schema分开,将其分为3个deffirent的位置。在我的项目root中,我创建utils文件夹,其中我以下面的方式存储文件:

utils
├── __init__.py
├── ...
└── swagger
   ├── __init__.py
   ├── generators.py            <---- http/https schema for swagger
   ├── response_templates.py    <---- templates of swagger_auto_schema response parts
   └── serializers.py           <---- some general response serializers

in response_templates.py有以下代码:

from drf_yasg import openapi
from rest_framework import status

from .serializers import DetailResponseSerializer, TaskRequestSerializer


FILE_SUCCESS_200 = {
    status.HTTP_200_OK: openapi.Response(
        description="Correct file get",
        schema=openapi.Schema(type=openapi.TYPE_FILE),
    ),
}

TASK_CREATED_201 = {
    status.HTTP_201_CREATED: openapi.Response(
        description="Successfully task created",
        schema=TaskRequestSerializer,
    ),
}

... # and other 

因此,我可以使用这些文件我的Swagger_Auto_Schema装饰器中的响应零件。下一步是在所有应用程序中创建响应文件夹,这些文件夹需要大肆宣传的自定义响应。响应树文件夹:

responses
├── __init__.py
├── image.py
└── and_other_models_name_files.py

image.py中有以下代码:

from utils.swagger.response_templates import (
    TASK_CREATED_201, FILE_SUCCESS_200, CREATED_201, SERIALIZE_400, NOT_FOUND_404,
    LARGE_ENTITY_413, INVALID_FILE_FORMAT_415, SERVER_STORAGE_500
)


image_list_responses = {
    **TASK_CREATED_201,
    **SERIALIZE_400,
    **LARGE_ENTITY_413,
    **INVALID_FILE_FORMAT_415,
}

image_detail_responses = {
    **FILE_SUCCESS_200,
    **NOT_FOUND_404,
    **SERVER_STORAGE_500
}

因此,由于上述重组,我可以在swagger> swagger_auto_schema_schema Decorator中使用上述响应。例子:

from media_app.responses import image_detail_responses


class DetailDownloadAPIView(RetrieveDestroyAPIView):
    @swagger_auto_schema(responses=image_detail_responses)
    def get(self, request, *args, **kwargs):
        ...

...

My solution is to separate responses parts, responses and swagger_auto_schema to 3 deffirent places. In my project root I've create utils folder, in which I store files in the following way:

utils
├── __init__.py
├── ...
└── swagger
   ├── __init__.py
   ├── generators.py            <---- http/https schema for swagger
   ├── response_templates.py    <---- templates of swagger_auto_schema response parts
   └── serializers.py           <---- some general response serializers

In response_templates.py there is following code:

from drf_yasg import openapi
from rest_framework import status

from .serializers import DetailResponseSerializer, TaskRequestSerializer


FILE_SUCCESS_200 = {
    status.HTTP_200_OK: openapi.Response(
        description="Correct file get",
        schema=openapi.Schema(type=openapi.TYPE_FILE),
    ),
}

TASK_CREATED_201 = {
    status.HTTP_201_CREATED: openapi.Response(
        description="Successfully task created",
        schema=TaskRequestSerializer,
    ),
}

... # and other 

So, I can use these response parts in my swagger_auto_schema decorator. The next step was to create responses folders in all applications, those needs custom responses in swagger. tree of responses folder:

responses
├── __init__.py
├── image.py
└── and_other_models_name_files.py

In image.py there is following code:

from utils.swagger.response_templates import (
    TASK_CREATED_201, FILE_SUCCESS_200, CREATED_201, SERIALIZE_400, NOT_FOUND_404,
    LARGE_ENTITY_413, INVALID_FILE_FORMAT_415, SERVER_STORAGE_500
)


image_list_responses = {
    **TASK_CREATED_201,
    **SERIALIZE_400,
    **LARGE_ENTITY_413,
    **INVALID_FILE_FORMAT_415,
}

image_detail_responses = {
    **FILE_SUCCESS_200,
    **NOT_FOUND_404,
    **SERVER_STORAGE_500
}

So, as a result of above restructure, I can use above responses in swagger_auto_schema decorator. Example:

from media_app.responses import image_detail_responses


class DetailDownloadAPIView(RetrieveDestroyAPIView):
    @swagger_auto_schema(responses=image_detail_responses)
    def get(self, request, *args, **kwargs):
        ...

...

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