【问题标题】:How to exclude show filters for specific endpoint in drf-yasg如何在 drf-yasg 中排除特定端点的显示过滤器
【发布时间】:2021-08-11 10:35:53
【问题描述】:

我有一些带有 filterset_fieldsordering_fields 属性的 ViewSet。此外,我在该 ViewSet 中有额外的操作,它用作通过一些过滤获取列表的快捷方式。我假设使用该额外操作而不处理任何额外的过滤器(或可能是订购)选项。但默认情况下,drf-yasg 使用 filterset_fieldsordering_fields 为该额外操作生成参数架构。

如何忽略特定端点的 filterset_fieldsordering_fields 属性?

【问题讨论】:

    标签: django django-rest-framework drf-yasg


    【解决方案1】:

    在您的操作装饰器中,将 filterset_fieldsordering_fields 设置为一个空列表:

    @action(detail=False, methods=['GET'], filterset_fields=[], ordering_fields=[], search_fields=[])
    

    您可以进一步禁用 filter_backends

    @action(detail=False, methods=['GET'], filter_backends=[])
    

    【讨论】:

      【解决方案2】:

      经过一些研究,我正在这样做:

      from drf_yasg.inspectors import SwaggerAutoSchema
      
      class MySwaggerAutoSchema(SwaggerAutoSchema):
          def get_query_parameters(self):
              return []
      

      然后在 swagger_auto_schema 装饰器中使用该子类来执行我的额外操作 @swagger_auto_schema(auto_schema=MySwaggerAutoSchema)

      但它看起来很野蛮。如果有人有更优雅的解决方案 - 请告诉我。

      【讨论】:

        猜你喜欢
        • 2022-11-02
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        • 2021-05-13
        • 2015-03-06
        • 2021-07-27
        • 2022-12-06
        • 2016-07-25
        相关资源
        最近更新 更多