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
class NoTitleAutoSchema(SwaggerAutoSchema):
field_inspectors = [NoSchemaTitleInspector] + >swagger_settings.DEFAULT_FIELD_INSPECTORS
class ArticleViewSet(viewsets.ModelViewSet):
swagger_schema = NoTitleAutoSchema
...
1条答案
按热度按时间k0pti3hp1#
根据我在drf_yasg docs中发现的内容,您需要实现一个名为 *
Inspector
classes* 的概念来自定义与特定字段、序列化程序、过滤器或分页器类相关的行为,您可以实现FieldInspector
、SerializerInspector
、FilterInspector
、PaginatorInspector
类,并将它们与@swagger_auto_schema
或related settings
之一一起使用。下面是一个
FieldInspector
示例,它从所有生成的Schema对象中删除title属性,并取自**Inspector
类**[drf_yasg-docs]: