You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用drf-yasg将指定URL从Django REST API文档中排除?

解决drf-yasg排除指定URL的几种方案

针对你要移除users/reset/<uidb64>/<token>/路径从swagger/redoc文档的需求,以下是几种实用的实现方式:

方案一:在schema_view中配置排除路径(推荐)

直接在get_schema_view里添加exclude_paths参数,用正则匹配要排除的URL路径,这是最直接的方式:

修改你的schema_view定义部分:

schema_view = get_schema_view(
   openapi.Info(
      title="title-test",
      default_version='v1',
      description="blah blah.",
   ),
   public=True,
   permission_classes=(permissions.IsAuthenticated,),
   # 添加该行,正则匹配所有以users/reset/开头的路径
   exclude_paths=r'^users/reset/.*$'
)

方案二:给目标视图添加排除装饰器

如果仅针对单个视图不想生成文档,可以给PasswordResetConfirmView加上swagger_auto_schema(exclude=True)装饰器。由于直接调用as_view()无法直接加装饰器,需要先自定义视图类:

  1. 先导入装饰器:
from drf_yasg.utils import swagger_auto_schema
  1. 自定义排除视图并修改URL配置:
# 用装饰器包裹原视图,标记为排除
@swagger_auto_schema(exclude=True)
class ExcludedPasswordResetConfirmView(PasswordResetConfirmView):
    pass

# 在urlpatterns中使用这个自定义视图
path('users/reset/<uidb64>/<token>/', ExcludedPasswordResetConfirmView.as_view()),

这样该视图就会被drf-yasg忽略,不会出现在生成的API文档中。

内容的提问来源于stack exchange,提问作者spidey677

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 06:25:21