add guidance for overriding get_tags method

This commit is contained in:
Dhaval Mehta 2020-02-19 22:49:45 +05:30
parent 22da4772c8
commit 95831b5be1

View File

@ -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