Experience Summary: Customizing a Few Details of drf-yasg API Documentation
drf-yasg is an extension for Django REST Framework that can generate OpenAPI interfaces from routes. Using it, however, is quite different from Java platforms and .NET Core, and some customizations are rather cumbersome. Still, looking at the docs is fine enough, and you can get things working. This article records a few issues encountered in actual development. Although they are details, polishing these details can make things much more convenient for colleagues integrating with the APIs~ By the way, writing backends in Python is really fast—can you believe dozens of APIs in one morning
目录9 个章节
drf-yasg is an extension for Django REST Framework that can generate OpenAPI interfaces from routes. Using it, however, is quite different from Java platforms and .NET Core, and some customizations are rather cumbersome. Still, looking at the docs is fine enough, and you can get things working.
This article records a few issues encountered in actual development. Although they are details, polishing these details can make things much more convenient for colleagues integrating with the APIs~
By the way, writing backends in Python is really fast—can you believe dozens of APIs in one morning?
Setting Request Parameters and API Descriptions
Implementation Code
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
required=['phone'],
properties={'phone': openapi.Schema(type=openapi.TYPE_STRING)}
), operation_summary='更新手机号码')
@action(detail=True, methods=['PUT'], permission_classes=[permissions.IsAuthenticated])
def phone(self, request, pk=None):
"""更新手机号码"""
if request.method == 'POST':
phone_num = request.data.get('phone')
profile_obj: UserProfile = get_object_or_404(UserProfile.objects.all(),
user=request.user)
profile_obj.phone = phone_num
profile_obj.save()
return Response({'detail': '更新手机号成功'})
Result

References
- Official documentation https://drf-yasg.readthedocs.io/en/stable/_modules/drf_yasg/inspectors/query.html?highlight=openapi.Parameter
- GitHub issues https://github.com/axnsan12/drf-yasg/issues/280
Adding a Description to Each Group
drf-yasg does not support this feature by default. This is a solution I found while browsing Stack Overflow—a bit of a roundabout workaround...
The Default State Looks Like This

Code
class CustomOpenAPISchemaGenerator(OpenAPISchemaGenerator):
"""重写 OpenAPISchemaGenerator 实现每个tag的说明文本"""
def get_schema(self, request=None, public=False):
swagger = super().get_schema(request, public)
swagger.tags = [
{
"name": "core",
"description": "核心功能",
},
{
'name': 'external_units',
'description': '外部单位'
},
{
'name': 'field_investigation',
'description': '现场勘查'
},
{
'name': 'require',
'description': '需求'
},
{
'name': 'require_delivered',
'description': '需求分配'
},
{
'name': 'scheme_appraisal',
'description': '方案评审'
}
]
return swagger
Result After the Change

References
_EOF_
Experience Summary: Customizing a Few Details of drf-yasg API Documentation
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
解决drf-yasg中的SwaggerAPI无法正确分组问题
swagger是后台开发中很好用的交互式文档,Django原本的Django-Swagger已经停止维护了,现在一般用drf_yasg这个包来实现文档,它里面支持swagger和redoc两种,redoc是静态的,作为导出文档的话不错,不过一般我们用swagger,因为可以在文档里面调试,非常方便。 Drf里面有个东西是AutoSchema,可以自动扫描ViewSet和APIView这类可以提供接
2022-04-04 · 0_Swagger与drf-yasg接口文档系列 · 约 1 分钟
Next.js路由段配置选项笔记
Next.js的路由段配置选项?真有这么简单?官方文档写得倒是分散,自己整理下倒也罢了。动态渲染还是静态?缓存策略又该怎么定?开发者还是得自己琢磨。
2025-10-30 · Web前端 · 约 1 分钟
Linux下查看dd命令写入进度
前言 Linux下有一个强力工具,dd,用来操作镜像,简直神器,唯一的不足是没有显示操作进度,不过也不是没有办法,下面介绍几个查看写入进度的方法。 status选项查看进度 如果你使用的是GNU版本的dd,并且coreutils版本高于8.24,那么可以使用status选项。例如: sudo dd if=/dev/sda of=/dev/zero status=progress 上面命令在Mac
2025-10-27 · FAQ & Tips · 约 1 分钟
Windows + AMD 显卡,终于能用 PyTorch 炼丹了
Windows+AMD+ROCm+PyTorch这地狱级组合居然跑通了。无非是用了官方ROCm和保守的PyTorch 2.9.1,避开新版本那些破事。下一步?总算能试试炼丹和AI画图了。
2026-05-09 · ROCm · 约 1 分钟
讨论
共 0 条加载评论中…