You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> 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.
6
+
>
7
+
> — Heroku, [JSON Schema for the Heroku Platform API][cite]
8
+
9
+
API schemas are a useful tool that allow for a range of use cases, including
10
+
generating reference documentation, or driving dynamic client libraries that
11
+
can interact with your API.
12
+
13
+
Django REST Framework provides support for automatic generation of
14
+
[OpenAPI][openapi] schemas.
15
+
3
16
## Generating an OpenAPI Schema
4
17
5
-
### Static
18
+
### Install `pyyaml`
19
+
20
+
You'll need to install `pyyaml`, so that you can render your generated schema
21
+
into the commonly used YAML-based OpenAPI format.
22
+
23
+
pip install pyyaml
24
+
25
+
### Generating a static schema with the `generateschema` management command
6
26
7
27
If your schema is static, you can use the `generateschema` management command:
8
28
9
29
```bash
10
30
./manage.py generateschema > openapi-schema.yml
11
31
```
12
32
13
-
### Dynamic
33
+
Once you've generated a schema in this way you can annotate it with any
34
+
additional information that cannot be automatically inferred by the schema
35
+
generator.
36
+
37
+
You might want to check your API schema into version control and update it
38
+
with each new release, or serve the API schema from your site's static media.
39
+
40
+
### Generating a dynamic schema with `SchemaView`
14
41
15
-
If you require a dynamic schema, because foreign key choices depend on database values, for example, you can route a `SchemaView` that will generate and serve you schema on demand.
42
+
If you require a dynamic schema, because foreign key choices depend on database
43
+
values, for example, you can route a `SchemaView` that will generate and serve
44
+
your schema on demand.
45
+
46
+
To route a `SchemaView`, use the `get_schema_view()` helper.
16
47
17
48
In `urls.py`:
18
49
@@ -34,19 +65,151 @@ urlpatterns = [
34
65
35
66
#### `get_schema_view()`
36
67
37
-
* ...
68
+
The `get_schema_view()` helper takes the following keyword arguments:
69
+
70
+
*`title`: May be used to provide a descriptive title for the schema definition.
71
+
*`description`: Longer descriptive text.
72
+
*`url`: May be used to pass a canonical base URL for the schema.
73
+
74
+
schema_view = get_schema_view(
75
+
title='Server Monitoring API',
76
+
url='https://www.example.org/api/'
77
+
)
78
+
79
+
*`urlconf`: A string representing the import path to the URL conf that you want
80
+
to generate an API schema for. This defaults to the value of Django's
81
+
`ROOT_URLCONF` setting.
82
+
83
+
schema_view = get_schema_view(
84
+
title='Server Monitoring API',
85
+
url='https://www.example.org/api/',
86
+
urlconf='myproject.urls'
87
+
)
88
+
*`patterns`: List of url patterns to limit the schema introspection to. If you
89
+
only want the `myproject.api` urls to be exposed in the schema:
0 commit comments