<p>A machine-readable [schema] describes what resources are available via the API, what their URLs are, how they are represented and what operations they support.</p>
<p>— Heroku, [JSON Schema for the Heroku Platform API][cite]</p>
</blockquote>
<p>API schemas are a useful tool that allow for a range of use cases, including
generating reference documentation, or driving dynamic client libraries that
can interact with your API.</p>
<p>Django REST Framework provides support for automatic generation of
<h3id="generating-a-static-schema-with-the-generateschema-management-command"><aclass="toclink"href="#generating-a-static-schema-with-the-generateschema-management-command">Generating a static schema with the <code>generateschema</code> management command</a></h3>
<p>If your schema is static, you can use the <code>generateschema</code> management command:</p>
<p>Once you've generated a schema in this way you can annotate it with any
additional information that cannot be automatically inferred by the schema
generator.</p>
<p>You might want to check your API schema into version control and update it
with each new release, or serve the API schema from your site's static media.</p>
<h3id="generating-a-dynamic-schema-with-schemaview"><aclass="toclink"href="#generating-a-dynamic-schema-with-schemaview">Generating a dynamic schema with <code>SchemaView</code></a></h3>
<p>If you require a dynamic schema, because foreign key choices depend on database
values, for example, you can route a <code>SchemaView</code> that will generate and serve
your schema on demand.</p>
<p>To route a <code>SchemaView</code>, use the <code>get_schema_view()</code> helper.</p>
<p>By default, view introspection is performed by an <code>AutoSchema</code> instance
accessible via the <code>schema</code> attribute on <code>APIView</code>. This provides the
appropriate <ahref="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operationObject">Open API operation object</a> for the view,
request method and path:</p>
<pre><code>auto_schema = view.schema
operation = auto_schema.get_operation(...)
</code></pre>
<p>In compiling the schema, <code>SchemaGenerator</code> calls <code>view.schema.get_operation()</code>
for each view, allowed method, and path.</p>
<hr/>
<p><strong>Note</strong>: For basic <code>APIView</code> subclasses, default introspection is essentially
limited to the URL kwarg path parameters. For <code>GenericAPIView</code>
subclasses, which includes all the provided class based views, <code>AutoSchema</code> will
attempt to introspect serializer, pagination and filter fields, as well as
provide richer path field descriptions. (The key hooks here are the relevant
<code>GenericAPIView</code> attributes and methods: <code>get_serializer</code>, <code>pagination_class</code>,
<code>filter_backends</code> and so on.)</p>
<hr/>
<p>In order to customize the operation generation, you should provide an <code>AutoSchema</code> subclass, overriding <code>get_operation()</code> as you need:</p>
<pre><code> from rest_framework.views import APIView
from rest_framework.schemas.openapi import AutoSchema
class CustomSchema(AutoSchema):
def get_operation(...):
# Implement custom introspection here (or in other sub-methods)
class CustomView(APIView):
"""APIView subclass with custom schema introspection."""
schema = CustomSchema()
</code></pre>
<p>This provides complete control over view introspection.</p>
<p>You may disable schema generation for a view by setting <code>schema</code> to <code>None</code>:</p>
<pre><code>class CustomView(APIView):
...
schema = None # Will not appear in schema
</code></pre>
<p>This also applies to extra actions for <code>ViewSet</code>s:</p>
<h3id="grouping-operations-with-tags"><aclass="toclink"href="#grouping-operations-with-tags">Grouping Operations With Tags</a></h3>
<p>Tags can be used to group logical operations. Each tag name in the list MUST be unique. </p>
<hr/>
<h4id="django-rest-framework-generates-tags-automatically-with-the-following-logic"><aclass="toclink"href="#django-rest-framework-generates-tags-automatically-with-the-following-logic">Django REST Framework generates tags automatically with the following logic:</a></h4>
<p>Tag name will be first element from the path. Also, any <code>_</code> in path name will be replaced by a <code>-</code>.
Consider below examples.</p>
<p>Example 1: Consider a user management system. The following table will illustrate the tag generation logic.
Here first element from the paths is: <code>users</code>. Hence tag wil be <code>users</code></p>
<table>
<thead>
<tr>
<th>Http Method</th>
<th>Path</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
<tr>
<td>PUT, PATCH, GET(Retrieve), DELETE</td>
<td>/users/{id}/</td>
<td>['users']</td>
</tr>
<tr>
<td>POST, GET(List)</td>
<td>/users/</td>
<td>['users']</td>
</tr>
</tbody>
</table>
<p>Example 2: Consider a restaurant management system. The System has restaurants. Each restaurant has branches.
Consider REST APIs to deal with a branch of a particular restaurant.
Here first element from the paths is: <code>restaurants</code>. Hence tag wil be <code>restaurants</code>.</p>
<p>Example 3: Consider Order items for an e commerce company.</p>
<table>
<thead>
<tr>
<th>Http Method</th>
<th>Path</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
<tr>
<td>PUT, PATCH, GET(Retrieve), DELETE</td>
<td>/order_items/{id}/</td>
<td>['order-items']</td>
</tr>
<tr>
<td>POST, GET(List)</td>
<td>/order_items/</td>
<td>['order-items']</td>
</tr>
</tbody>
</table>
<hr/>
<h4id="overriding-auto-generated-tags"><aclass="toclink"href="#overriding-auto-generated-tags">Overriding auto generated tags:</a></h4>
<p>You can override auto-generated tags by passing <code>tags</code> argument to the constructor of <code>AutoSchema</code>. <code>tags</code> argument must be a list or tuple of string.</p>
<p>If you need more customization, you can override the <code>get_tags</code> method of <code>AutoSchema</code> class. Consider the following example:</p>
<p>The schema generator generates an <ahref="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-17">operationid</a> for each operation. This <code>operationId</code> is deduced from the model name, serializer name or view name. The operationId may looks like "listItems", "retrieveItem", "updateItem", etc..
The <code>operationId</code> is camelCase by convention.</p>
<p>If you have several views with the same model, the generator may generate duplicate operationId.
In order to work around this, you can override the second part of the operationId: operation name.</p>
<p>The previous example will generate the following operationId: "listCustoms", "retrieveCustom", "updateCustom", "partialUpdateCustom", "destroyCustom".
You need to provide the singular form of he operation name. For the list operation, a "s" will be appended at the end of the operation.</p>
<p>If you need more configuration over the <code>operationId</code> field, you can override the <code>get_operation_id_base</code> and <code>get_operation_id</code> methods from the <code>AutoSchema</code> class:</p>
<p>Since DRF 3.12, Schema uses the <ahref="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#componentsObject">OpenAPI Components</a>. This method defines components in the schema and <ahref="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#referenceObject">references them</a> inside request and response objects. By default, the component's name is deduced from the Serializer's name.</p>
<p>Using OpenAPI's components provides the following advantages:</p>
<ul>
<li>The schema is more readable and lightweight.</li>
<li>If you use the schema to generate an SDK (using <ahref="https://github.com/OpenAPITools/openapi-generator">openapi-generator</a> or <ahref="https://github.com/swagger-api/swagger-codegen">swagger-codegen</a>). The generator can name your SDK's models.</li>
<p>You may get the following error while generating the schema:</p>
<pre><code>"Serializer" is an invalid class name for schema generation.
Serializer's class name should be unique and explicit. e.g. "ItemSerializer".
</code></pre>
<p>This error occurs when the Serializer name is "Serializer". You should choose a component's name unique across your schema and different than "Serializer".</p>
<p>You may also get the following warning:</p>
<pre><code>Schema component "ComponentName" has been overriden with a different value.
</code></pre>
<p>This warning occurs when different components have the same name in one schema. Your component name should be unique across your project. This is likely an error that may lead to an invalid schema.</p>
<p>You have two ways to solve the previous issues:</p>
<ul>
<li>You can rename your serializer with a unique name and another name than "Serializer".</li>
<li>You can set the <code>component_name</code> kwarg parameter of the AutoSchema constructor (see below).</li>
<li>You can override the <code>get_component_name</code> method of the AutoSchema class (see below).</li>
</ul>
<h4id="set-a-custom-components-name-for-your-view"><aclass="toclink"href="#set-a-custom-components-name-for-your-view">Set a custom component's name for your view</a></h4>
<p>To override the component's name in your view, you can use the <code>component_name</code> parameter of the AutoSchema constructor:</p>
<h4id="override-the-default-implementation"><aclass="toclink"href="#override-the-default-implementation">Override the default implementation</a></h4>
<p>If you want to have more control and customization about how the schema's components are generated, you can override the <code>get_component_name</code> and <code>get_components</code> method from the AutoSchema class.</p>