简体   繁体   English

如何在 drf-yasg 中排除特定端点的显示过滤器

[英]How to exclude show filters for specific endpoint in drf-yasg

I have some ViewSet with filterset_fields and ordering_fields attributes.我有一些带有filterset_fieldsordering_fields属性的 ViewSet。 Also i have extra action in that ViewSet, that uses as a shortcut to get list with some filtration.此外,我在该 ViewSet 中有额外的操作,它用作通过一些过滤获取列表的快捷方式。 I assume to use that extra action without handling any additional filter(or may be ordering) options.我假设使用该额外操作而不处理任何额外的过滤器(或可能是订购)选项。 But in default way drf-yasg genereates parameters schema for that extra action with filterset_fields and ordering_fields .但默认情况下, drf-yasg 使用filterset_fieldsordering_fields为该额外操作生成参数模式。

How i can ignore filterset_fields and ordering_fields attributes for specific endpoint?我如何忽略特定端点的filterset_fieldsordering_fields属性?

After some research i'm do that:经过一些研究,我这样做:

from drf_yasg.inspectors import SwaggerAutoSchema

class MySwaggerAutoSchema(SwaggerAutoSchema):
    def get_query_parameters(self):
        return []

And use that subclass in swagger_auto_schema decarator for my extra action that way @swagger_auto_schema(auto_schema=MySwaggerAutoSchema) .并在swagger_auto_schema说明符中使用该子类来执行我的额外操作@swagger_auto_schema(auto_schema=MySwaggerAutoSchema)

But it looks barbaric.但它看起来很野蛮。 If anyone has a more elegant solution - let me know.如果有人有更优雅的解决方案 - 请告诉我。

In your action decorator set filterset_fields and ordering_fields to an empty list:在您的操作装饰器中,将filterset_fieldsordering_fields设置为一个空列表:

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

You can go even further and disable filter_backends :您可以进一步 go 并禁用filter_backends

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM