Skip to content

Commit 2befa6c

Browse files
author
Carlton Gibson
authored
Document documentation.py (#5478)
Closes #5198. Closes #5142. Closes #4980
1 parent cc7f4f5 commit 2befa6c

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

docs/topics/documenting-your-api.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ This will include two different views:
3434
* `/docs/` - The documentation page itself.
3535
* `/docs/schema.js` - A JavaScript resource that exposes the API schema.
3636

37+
---
38+
39+
**Note**: By default `include_docs_urls` configures the underlying `SchemaView` to generate _public_ schemas.
40+
This means that views will not be instantiated with a `request` instance. i.e. Inside the view `self.request` will be `None`.
41+
42+
To be compatible with this behaviour methods (such as `get_serializer` or `get_serializer_class` etc.) which inspect `self.request` or, particularly, `self.request.user` may need to be adjusted to handle this case.
43+
44+
You may ensure views are given a `request` instance by calling `include_docs_urls` with `public=False`:
45+
46+
from rest_framework.documentation import include_docs_urls
47+
48+
urlpatterns = [
49+
...
50+
# Generate schema with valid `request` instance:
51+
url(r'^docs/', include_docs_urls(title='My API title', public=False))
52+
]
53+
54+
55+
---
56+
57+
3758
### Documenting your views
3859

3960
You can document your views by including docstrings that describe each of the available actions.
@@ -69,6 +90,46 @@ When using viewsets, you should use the relevant action names as delimiters.
6990
Create a new user instance.
7091
"""
7192

93+
94+
### `documentation` API Reference
95+
96+
The `rest_framework.documentation` module provides three helper functions to help configure the interactive API documentation, `include_docs_url` (usage shown above), `get_docs_view` and `get_schemajs_view`.
97+
98+
`include_docs_url` employs `get_docs_view` and `get_schemajs_view` to generate the url patterns for the documentation page and JavaScript resource that exposes the API schema respectively. They expose the following options for customisation. (`get_docs_view` and `get_schemajs_view` ultimately call `rest_frameworks.schemas.get_schema_view()`, see the Schemas docs for more options there.)
99+
100+
#### `include_docs_url`
101+
102+
* `title`: Default `None`. May be used to provide a descriptive title for the schema definition.
103+
* `description`: Default `None`. May be used to provide a description for the schema definition.
104+
* `schema_url`: Default `None`. May be used to pass a canonical base URL for the schema.
105+
* `public`: Default `True`. Should the schema be considered _public_? If `True` schema is generated without a `request` instance being passed to views.
106+
* `patterns`: Default `None`. A list of URLs to inspect when generating the schema. If `None` project's URL conf will be used.
107+
* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
108+
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
109+
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
110+
111+
#### `get_docs_view`
112+
113+
* `title`: Default `None`. May be used to provide a descriptive title for the schema definition.
114+
* `description`: Default `None`. May be used to provide a description for the schema definition.
115+
* `schema_url`: Default `None`. May be used to pass a canonical base URL for the schema.
116+
* `public`: Default `True`. If `True` schema is generated without a `request` instance being passed to views.
117+
* `patterns`: Default `None`. A list of URLs to inspect when generating the schema. If `None` project's URL conf will be used.
118+
* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
119+
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
120+
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
121+
122+
#### `get_schemajs_view`
123+
124+
* `title`: Default `None`. May be used to provide a descriptive title for the schema definition.
125+
* `description`: Default `None`. May be used to provide a description for the schema definition.
126+
* `schema_url`: Default `None`. May be used to pass a canonical base URL for the schema.
127+
* `public`: Default `True`. If `True` schema is generated without a `request` instance being passed to views.
128+
* `patterns`: Default `None`. A list of URLs to inspect when generating the schema. If `None` project's URL conf will be used.
129+
* `generator_class`: Default `rest_framework.schemas.SchemaGenerator`. May be used to specify a `SchemaGenerator` subclass to be passed to the `SchemaView`.
130+
* `authentication_classes`: Default `api_settings.DEFAULT_AUTHENTICATION_CLASSES`. May be used to pass custom authentication classes to the `SchemaView`.
131+
* `permission_classes`: Default `api_settings.DEFAULT_PERMISSION_CLASSES` May be used to pass custom permission classes to the `SchemaView`.
132+
72133
---
73134

74135
## Third party packages
@@ -230,4 +291,4 @@ To implement a hypermedia API you'll need to decide on an appropriate media type
230291
[image-django-rest-swagger]: ../img/django-rest-swagger.png
231292
[image-apiary]: ../img/apiary.png
232293
[image-self-describing-api]: ../img/self-describing.png
233-
[schemas-examples]: ../api-guide/schemas/#examples
294+
[schemas-examples]: ../api-guide/schemas/#example

0 commit comments

Comments
 (0)