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 分钟
绿联USB3.0拓展坞开箱
之前家里用的USB拓展坞是USB2.0的好像 这次我就没带过来 于是张罗着买个USB3.0以上的新拓展坞来用 看了下3.0和3.2价格差距有点多,只是用来插鼠标和键盘适配器的话,似乎不需要那么高的速度,所以入手了绿联3.0的拓展坞
2026-09-08 · 拓展坞 · 约 1 分钟
第一次用65配列,RK-R65键盘开箱
RK-R65键盘,颜值尚可,重量712g,勉强接受。烟青轴手感不错,麻将音悦耳,但无F1-F12键,方向键无间隔,定位困难。驱动奇怪,方向键功能写死,fn2+无效,啧。尝试适应65配列,看能否习惯。
2025-10-28 · 键盘 · 约 1 分钟
开启GitLab的邮件通知功能以及一些外观配置
这次把邮件功能、域名、外观啥的配置好了,写篇文章记录一下
2024-07-17 · DevOps · 约 1 分钟
讨论
共 0 条加载评论中…