| 1 |
<style> |
| 2 |
.promo li a { |
| 3 |
float: left; |
| 4 |
width: 130px; |
| 5 |
height: 20px; |
| 6 |
text-align: center; |
| 7 |
margin: 10px 30px; |
| 8 |
padding: 150px 0 0 0; |
| 9 |
background-position: 0 50%; |
| 10 |
background-size: 130px auto; |
| 11 |
background-repeat: no-repeat; |
| 12 |
font-size: 120%; |
| 13 |
color: black; |
| 14 |
} |
| 15 |
.promo li { |
| 16 |
list-style: none; |
| 17 |
} |
| 18 |
</style> |
| 19 |
|
| 20 |
# Django REST framework 3.9 |
| 21 |
|
| 22 |
The 3.9 release gives access to _extra actions_ in the Browsable API, introduces composable permissions and built-in [OpenAPI][openapi] schema support. (Formerly known as Swagger) |
| 23 |
|
| 24 |
--- |
| 25 |
|
| 26 |
## Funding |
| 27 |
|
| 28 |
If you use REST framework commercially and would like to see this work continue, we strongly encourage you to invest in its continued development by |
| 29 |
**[signing up for a paid plan][funding]**. |
| 30 |
|
| 31 |
|
| 32 |
<ul class="premium-promo promo"> |
| 33 |
<li><a href="https://www.rover.com/careers/" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/rover_130x130.png)">Rover.com</a></li> |
| 34 |
<li><a href="https://sentry.io/welcome/" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/sentry130.png)">Sentry</a></li> |
| 35 |
<li><a href="https://getstream.io/try-the-api/?utm_source=drf&utm_medium=banner&utm_campaign=drf" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/stream-130.png)">Stream</a></li> |
| 36 |
<li><a href="https://auklet.io" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/auklet-new.png)">Auklet</a></li> |
| 37 |
<li><a href="https://rollbar.com" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/rollbar2.png)">Rollbar</a></li> |
| 38 |
<li><a href="https://cadre.com" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/cadre.png)">Cadre</a></li> |
| 39 |
<li><a href="https://loadimpact.com/?utm_campaign=Sponsorship%20links&utm_source=drf&utm_medium=drf" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/load-impact.png)">Load Impact</a></li> |
| 40 |
<li><a href="https://hubs.ly/H0f30Lf0" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/kloudless.png)">Kloudless</a></li> |
| 41 |
</ul> |
| 42 |
<div style="clear: both; padding-bottom: 20px;"></div> |
| 43 |
|
| 44 |
*Many thanks to all our [wonderful sponsors][sponsors], and in particular to our premium backers, [Rover](https://www.rover.com/careers/), [Sentry](https://sentry.io/welcome/), [Stream](https://getstream.io/?utm_source=drf&utm_medium=banner&utm_campaign=drf), [Auklet](https://auklet.io/), [Rollbar](https://rollbar.com), [Cadre](https://cadre.com), [Load Impact](https://loadimpact.com/?utm_campaign=Sponsorship%20links&utm_source=drf&utm_medium=drf), and [Kloudless](https://hubs.ly/H0f30Lf0).* |
| 45 |
|
| 46 |
--- |
| 47 |
|
| 48 |
## Built-in OpenAPI schema support |
| 49 |
|
| 50 |
REST framework now has a first-pass at directly including OpenAPI schema support. (Formerly known as Swagger) |
| 51 |
|
| 52 |
Specifically: |
| 53 |
|
| 54 |
* There are now `OpenAPIRenderer`, and `JSONOpenAPIRenderer` classes that deal with encoding `coreapi.Document` instances into OpenAPI YAML or OpenAPI JSON. |
| 55 |
* The `get_schema_view(...)` method now defaults to OpenAPI YAML, with CoreJSON as a secondary |
| 56 |
option if it is selected via HTTP content negotiation. |
| 57 |
* There is a new management command `generateschema`, which you can use to dump |
| 58 |
the schema into your repository. |
| 59 |
|
| 60 |
Here's an example of adding an OpenAPI schema to the URL conf: |
| 61 |
|
| 62 |
```python |
| 63 |
from rest_framework.schemas import get_schema_view |
| 64 |
from rest_framework.renderers import JSONOpenAPIRenderer |
| 65 |
from django.urls import path |
| 66 |
|
| 67 |
schema_view = get_schema_view( |
| 68 |
title="Server Monitoring API", |
| 69 |
url="https://www.example.org/api/", |
| 70 |
renderer_classes=[JSONOpenAPIRenderer], |
| 71 |
) |
| 72 |
|
| 73 |
urlpatterns = [path("schema.json", schema_view), ...] |
| 74 |
``` |
| 75 |
|
| 76 |
And here's how you can use the `generateschema` management command: |
| 77 |
|
| 78 |
```shell |
| 79 |
$ python manage.py generateschema --format openapi > schema.yml |
| 80 |
``` |
| 81 |
|
| 82 |
There's lots of different tooling that you can use for working with OpenAPI |
| 83 |
schemas. One option that we're working on is the [API Star](https://docs.apistar.com/) |
| 84 |
command line tool. |
| 85 |
|
| 86 |
You can use `apistar` to validate your API schema: |
| 87 |
|
| 88 |
```shell |
| 89 |
$ apistar validate --path schema.json --format openapi |
| 90 |
✓ Valid OpenAPI schema. |
| 91 |
``` |
| 92 |
|
| 93 |
Or to build API documentation: |
| 94 |
|
| 95 |
```shell |
| 96 |
$ apistar docs --path schema.json --format openapi |
| 97 |
✓ Documentation built at "build/index.html". |
| 98 |
``` |
| 99 |
|
| 100 |
API Star also includes a [dynamic client library](https://docs.apistar.com/client-library/) |
| 101 |
that uses an API schema to automatically provide a client library interface for making requests. |
| 102 |
|
| 103 |
## Composable permission classes |
| 104 |
|
| 105 |
You can now compose permission classes using the and/or operators, `&` and `|`. |
| 106 |
|
| 107 |
For example... |
| 108 |
|
| 109 |
```python |
| 110 |
permission_classes = [IsAuthenticated & (ReadOnly | IsAdminUser)] |
| 111 |
``` |
| 112 |
|
| 113 |
If you're using custom permission classes then make sure that you are subclassing |
| 114 |
from `BasePermission` in order to enable this support. |
| 115 |
|
| 116 |
## ViewSet _Extra Actions_ available in the Browsable API |
| 117 |
|
| 118 |
Following the introduction of the `action` decorator in v3.8, _extra actions_ defined on a ViewSet are now available |
| 119 |
from the Browsable API. |
| 120 |
|
| 121 |
 |
| 122 |
|
| 123 |
When defined, a dropdown of "Extra Actions", appropriately filtered to detail/non-detail actions, is displayed. |
| 124 |
|
| 125 |
--- |
| 126 |
|
| 127 |
## Supported Versions |
| 128 |
|
| 129 |
REST framework 3.9 supports Django versions 1.11, 2.0, and 2.1. |
| 130 |
|
| 131 |
--- |
| 132 |
|
| 133 |
## Deprecations |
| 134 |
|
| 135 |
### `DjangoObjectPermissionsFilter` moved to third-party package. |
| 136 |
|
| 137 |
The `DjangoObjectPermissionsFilter` class is pending deprecation, will be deprecated in 3.10 and removed entirely in 3.11. |
| 138 |
|
| 139 |
It has been moved to the third-party [`djangorestframework-guardian`](https://github.com/rpkilby/django-rest-framework-guardian) |
| 140 |
package. Please use this instead. |
| 141 |
|
| 142 |
### Router argument/method renamed to use `basename` for consistency. |
| 143 |
|
| 144 |
* The `Router.register` `base_name` argument has been renamed in favor of `basename`. |
| 145 |
* The `Router.get_default_base_name` method has been renamed in favor of `Router.get_default_basename`. [#5990][gh5990] |
| 146 |
|
| 147 |
See [#5990][gh5990]. |
| 148 |
|
| 149 |
[gh5990]: https://github.com/encode/django-rest-framework/pull/5990 |
| 150 |
|
| 151 |
`base_name` and `get_default_base_name()` are pending deprecation. They will be deprecated in 3.10 and removed entirely in 3.11. |
| 152 |
|
| 153 |
### `action` decorator replaces `list_route` and `detail_route` |
| 154 |
|
| 155 |
Both `list_route` and `detail_route` are now deprecated in favor of the single `action` decorator. |
| 156 |
They will be removed entirely in 3.10. |
| 157 |
|
| 158 |
The `action` decorator takes a boolean `detail` argument. |
| 159 |
|
| 160 |
* Replace `detail_route` uses with `@action(detail=True)`. |
| 161 |
* Replace `list_route` uses with `@action(detail=False)`. |
| 162 |
|
| 163 |
### `exclude_from_schema` |
| 164 |
|
| 165 |
Both `APIView.exclude_from_schema` and the `exclude_from_schema` argument to the `@api_view` have now been removed. |
| 166 |
|
| 167 |
For `APIView` you should instead set a `schema = None` attribute on the view class. |
| 168 |
|
| 169 |
For function-based views the `@schema` decorator can be used to exclude the view from the schema, by using `@schema(None)`. |
| 170 |
|
| 171 |
--- |
| 172 |
|
| 173 |
## Minor fixes and improvements |
| 174 |
|
| 175 |
There are a large number of minor fixes and improvements in this release. See the [release notes](release-notes.md) page for a complete listing. |
| 176 |
|
| 177 |
|
| 178 |
## What's next |
| 179 |
|
| 180 |
We're planning to iteratively work towards OpenAPI becoming the standard schema |
| 181 |
representation. This will mean that the `coreapi` dependency will gradually become |
| 182 |
removed, and we'll instead generate the schema directly, rather than building |
| 183 |
a CoreAPI `Document` object. |
| 184 |
|
| 185 |
OpenAPI has clearly become the standard for specifying Web APIs, so there's not |
| 186 |
much value any more in our schema-agnostic document model. Making this change |
| 187 |
will mean that we'll more easily be able to take advantage of the full set of |
| 188 |
OpenAPI functionality. |
| 189 |
|
| 190 |
This will also make a wider range of tooling available. |
| 191 |
|
| 192 |
We'll focus on continuing to develop the [API Star](https://docs.apistar.com/) |
| 193 |
library and client tool into a recommended option for generating API docs, |
| 194 |
validating API schemas, and providing a dynamic client library. |
| 195 |
|
| 196 |
There's also a huge amount of ongoing work on maturing the ASGI landscape, |
| 197 |
with the possibility that some of this work will eventually [feed back into |
| 198 |
Django](https://www.aeracode.org/2018/06/04/django-async-roadmap/). |
| 199 |
|
| 200 |
There will be further work on the [Uvicorn](https://www.uvicorn.org/) |
| 201 |
web server, as well as lots of functionality planned for the [Starlette](https://www.starlette.io/) |
| 202 |
web framework, which is building a foundational set of tooling for working with |
| 203 |
ASGI. |
| 204 |
|
| 205 |
|
| 206 |
[funding]: https://opencollective.com/django-rest-framework |
| 207 |
[gh5886]: https://github.com/encode/django-rest-framework/issues/5886 |
| 208 |
[gh5705]: https://github.com/encode/django-rest-framework/issues/5705 |
| 209 |
[openapi]: https://www.openapis.org/ |
| 210 |
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors |