Django drf-spectaulous - 可以排除特定路径吗?

Gar*_*eth 11 django swagger drf-spectacular

我们在 urls.py 中有一堆不同版本的 api,例如

  • API/v1
  • API/v2
  • API/v3

。我们想用 drf-spectaulous 来实现 swagger,但我们只想公开 api/v3 端点。

有没有办法做到这一点?我无法理解文档。

谢谢

Chr*_*ris 16

这对我有用:

def preprocessing_filter_spec(endpoints):
    filtered = []
    for (path, path_regex, method, callback) in endpoints:
        # Remove all but DRF API endpoints
        if path.startswith("/api/"):
            filtered.append((path, path_regex, method, callback))
    return filtered
Run Code Online (Sandbox Code Playgroud)

在设置中:

"PREPROCESSING_HOOKS": ["common.openapi.preprocessing_filter_spec"],
Run Code Online (Sandbox Code Playgroud)


jfu*_*unk 8

您还可以使用以下命令排除它:


    from drf_spectacular.utils import extend_schema
    ...
    
    @extend_schema(
        exclude=True
    )
    @api_view(['GET'])
    def my_view(request):
        # your code here
Run Code Online (Sandbox Code Playgroud)

或者您可以排除整个视图集:


    @extend_schema_view(
        list=extend_schema(exclude=True),
        retrieve=extend_schema(exclude=True),
        create=extend_schema(exclude=True),
        update=extend_schema(exclude=True),
        partial_update=extend_schema(exclude=True),
        destroy=extend_schema(exclude=True)
    )
Run Code Online (Sandbox Code Playgroud)


Gar*_*eth 0

解决了。复制自定义预处理挂钩函数,更改 pass 语句以过滤端点中不需要的内容,然后在我的预处理挂钩的壮观设置中映射文件和函数的位置。