From 95831b5be1bf0804d82d25f8d5cb809d4aff43f5 Mon Sep 17 00:00:00 2001 From: Dhaval Mehta <20968146+dhaval-mehta@users.noreply.github.com> Date: Wed, 19 Feb 2020 22:49:45 +0530 Subject: [PATCH] add guidance for overriding get_tags method --- docs/api-guide/schemas.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/schemas.md b/docs/api-guide/schemas.md index 2f0ba7f2e..e2d6b2060 100644 --- a/docs/api-guide/schemas.md +++ b/docs/api-guide/schemas.md @@ -221,7 +221,7 @@ project you may adjust `settings.DEFAULT_SCHEMA_CLASS` appropriately. Tags can be used to group logical operations. Each tag name in the list MUST be unique. --- -#### Django REST Framework generates tags automatically with following logic: +#### Django REST Framework generates tags automatically with the following logic: 1. Extract tag from `ViewSet`. 1. If `ViewSet` name ends with `ViewSet`, or `View`; remove it. @@ -277,6 +277,32 @@ class MyView(APIView): ... ``` +If you need more customization, you can override the `get_tags` method of `AutoSchema` class. Consider the following example: + +```python +from rest_framework.schemas.openapi import AutoSchema +from rest_framework.views import APIView + +class MySchema(AutoSchema): + ... + def get_tags(self, path, method): + if method == 'POST': + tags = ['tag1', 'tag2'] + elif method == 'GET': + tags = ['tag2', 'tag3'] + elif path == '/example/path/': + tags = ['tag3', 'tag4'] + else: + tags = ['tag5', 'tag6', 'tag7'] + + return tags + +class MyView(APIView): + schema = MySchema() + ... +``` + + [openapi]: https://github.com/OAI/OpenAPI-Specification [openapi-specification-extensions]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#specification-extensions [openapi-operation]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operationObject