@@ -215,6 +215,81 @@ This also applies to extra actions for `ViewSet`s:
215
215
If you wish to provide a base ` AutoSchema ` subclass to be used throughout your
216
216
project you may adjust ` settings.DEFAULT_SCHEMA_CLASS ` appropriately.
217
217
218
+
219
+ ### Grouping Operations With Tags
220
+
221
+ Tags can be used to group logical operations. Each tag name in the list MUST be unique.
222
+
223
+ ---
224
+ #### Django REST Framework generates tags automatically with the following logic:
225
+
226
+ Tag name will be first element from the path. Also, any ` _ ` in path name will be replaced by a ` - ` .
227
+ Consider below examples.
228
+
229
+ Example 1: Consider a user management system. The following table will illustrate the tag generation logic.
230
+ Here first element from the paths is: ` users ` . Hence tag wil be ` users `
231
+
232
+ Http Method | Path | Tags
233
+ -------------------------------------|-------------------|-------------
234
+ PUT, PATCH, GET(Retrieve), DELETE | /users/{id}/ | [ 'users']
235
+ POST, GET(List) | /users/ | [ 'users']
236
+
237
+ Example 2: Consider a restaurant management system. The System has restaurants. Each restaurant has branches.
238
+ Consider REST APIs to deal with a branch of a particular restaurant.
239
+ Here first element from the paths is: ` restaurants ` . Hence tag wil be ` restaurants ` .
240
+
241
+ Http Method | Path | Tags
242
+ -------------------------------------|----------------------------------------------------|-------------------
243
+ PUT, PATCH, GET(Retrieve), DELETE: | /restaurants/{restaurant_id}/branches/{branch_id} | [ 'restaurants']
244
+ POST, GET(List): | /restaurants/{restaurant_id}/branches/ | [ 'restaurants']
245
+
246
+ Example 3: Consider Order items for an e commerce company.
247
+
248
+ Http Method | Path | Tags
249
+ -------------------------------------|-------------------------|-------------
250
+ PUT, PATCH, GET(Retrieve), DELETE | /order_items/{id}/ | [ 'order-items']
251
+ POST, GET(List) | /order_items/ | [ 'order-items']
252
+
253
+
254
+ ---
255
+ #### Overriding auto generated tags:
256
+ You can override auto-generated tags by passing ` tags ` argument to the constructor of ` AutoSchema ` . ` tags ` argument must be a list or tuple of string.
257
+ ``` python
258
+ from rest_framework.schemas.openapi import AutoSchema
259
+ from rest_framework.views import APIView
260
+
261
+ class MyView (APIView ):
262
+ schema = AutoSchema(tags = [' tag1' , ' tag2' ])
263
+ ...
264
+ ```
265
+
266
+ If you need more customization, you can override the ` get_tags ` method of ` AutoSchema ` class. Consider the following example:
267
+
268
+ ``` python
269
+ from rest_framework.schemas.openapi import AutoSchema
270
+ from rest_framework.views import APIView
271
+
272
+ class MySchema (AutoSchema ):
273
+ ...
274
+ def get_tags (self , path , method ):
275
+ if method == ' POST' :
276
+ tags = [' tag1' , ' tag2' ]
277
+ elif method == ' GET' :
278
+ tags = [' tag2' , ' tag3' ]
279
+ elif path == ' /example/path/' :
280
+ tags = [' tag3' , ' tag4' ]
281
+ else :
282
+ tags = [' tag5' , ' tag6' , ' tag7' ]
283
+
284
+ return tags
285
+
286
+ class MyView (APIView ):
287
+ schema = MySchema()
288
+ ...
289
+ ```
290
+
291
+
218
292
[ openapi ] : https://github.com/OAI/OpenAPI-Specification
219
293
[ openapi-specification-extensions ] : https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#specification-extensions
220
294
[ openapi-operation ] : https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operationObject
295
+ [ openapi-tags ] : https://swagger.io/specification/#tagObject
0 commit comments