drf_yasg swagger_auto_schema从视图移动
在我的项目视图类中,我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的解决方案是将响应零件,响应和Swagger_Auto_Schema分开,将其分为3个deffirent的位置。在我的项目root中,我创建
utils
文件夹,其中我以下面的方式存储文件:in
response_templates.py
有以下代码:因此,我可以使用这些文件我的
Swagger_Auto_Schema
装饰器中的响应零件。下一步是在所有应用程序中创建响应
文件夹,这些文件夹需要大肆宣传的自定义响应。响应树文件夹:在
image.py
中有以下代码:因此,由于上述重组,我可以在
swagger> swagger_auto_schema_schema
Decorator中使用上述响应。例子: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:In
response_templates.py
there is following code:So, I can use these response parts in my
swagger_auto_schema
decorator. The next step was to createresponses
folders in all applications, those needs custom responses in swagger. tree of responses folder:In
image.py
there is following code:So, as a result of above restructure, I can use above responses in
swagger_auto_schema
decorator. Example: