简体   繁体   English

drf-yasg 隐藏标题属性

[英]drf-yasg hide title property

So as shown in the image, is there any way to hide the 'title' property foreach item?所以如图所示,有没有办法隐藏每个项目的“标题”属性? I don't really see the purpose of these titles.我真的不明白这些标题的目的。

Based on drf_yasg documentation you should first define a class to have a schema without title for each API you can define a class as below:根据drf_yasg 文档,您应该首先定义一个类来为每个 API 定义一个没有标题架构,您可以定义一个类,如下所示:

from drf_yasg.inspectors import FieldInspector

class NoSchemaTitleInspector(FieldInspector):
   def process_result(self, result, method_name, obj, **kwargs):
      # remove the `title` attribute of all Schema objects
      if isinstance(result, openapi.Schema.OR_REF):
         # traverse any references and alter the Schema object in place
         schema = openapi.resolve_ref(result, self.components)
         schema.pop('title', None)

         # no ``return schema`` here, because it would mean we always generate
         # an inline `object` instead of a definition reference

      # return back the same object that we got - i.e. a reference if we got a reference
      return result

Now you should define a class to use drf_yasg for removing title automatically , therefore you need to define a class like below:现在您应该定义一个类来使用drf_yasg自动删除标题,因此您需要定义一个如下所示的类:

class NoTitleAutoSchema(SwaggerAutoSchema):
   field_inspectors = [NoSchemaTitleInspector] + swagger_settings.DEFAULT_FIELD_INSPECTORS

Here like the below snippet you can hide all title properties from your API viewset:像下面的代码片段一样,您可以从 API 视图集中隐藏所有标题属性:

class ArticleViewSet(viewsets.ModelViewSet):
   swagger_schema = NoTitleAutoSchema
   ...

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

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