簡體   English   中英

drf-yasg 隱藏標題屬性

[英]drf-yasg hide title property

所以如圖所示,有沒有辦法隱藏每個項目的“標題”屬性? 我真的不明白這些標題的目的。

根據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

現在您應該定義一個類來使用drf_yasg自動刪除標題,因此您需要定義一個如下所示的類:

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

像下面的代碼片段一樣,您可以從 API 視圖集中隱藏所有標題屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM