从 Django REST Swagger 中排除端点 HTTP 方法



有没有办法只隐藏端点的某些方法而不是整个端点?(例如,显示 POST 方法但隐藏 DELETE 方法(

我尝试使用AutoSchema自定义文档的地方

例如,像这样的端点

router.register(r'audittrial', AuditTrialViewSet, 'AuditTrial')

将定义以下架构

class AuditTrialCustomView(AutoSchema):
@staticmethod
def get_field(name, required, location, schema, description):
return coreapi.Field(
name=name,
required=required,
location=location,
schema=schema,
description=description
)
def get_manual_fields(self, path, method):
extra_fields = []
if method == 'GET':
extra_fields = [
self.get_field("from", False, "query", coreschema.String(), "Date of the start of the Audit Trial"),
....
]
return extra_fields

有什么方法可以做到这一点吗?

DRF有以下示例 - 看看它是否对您有帮助。

class CustomAutoSchema(AutoSchema):
def get_link(self, path, method, base_url):
# override view introspection here...
@api_view(['GET'])
@schema(CustomAutoSchema())
def view(request):
return Response({"message": "Hello for today! See you tomorrow!"})

这样api_view装饰器应该可以帮助您。 它将列表中的方法列表作为参数。

最新更新