|
| 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="http://jobs.rover.com/" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/rover_130x130.png)">Rover.com</a></li> |
| 34 | + <li><a href="https://getsentry.com/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](http://jobs.rover.com/), [Sentry](https://getsentry.com/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 | + |
| 66 | +schema_view = get_schema_view( |
| 67 | + title='Server Monitoring API', |
| 68 | + url='https://www.example.org/api/', |
| 69 | + renderer_classes=[JSONOpenAPIRenderer] |
| 70 | +) |
| 71 | + |
| 72 | +urlpatterns = [ |
| 73 | + url('^schema.json$', schema_view), |
| 74 | + ... |
| 75 | +] |
| 76 | +``` |
| 77 | + |
| 78 | +And here's how you can use the `generateschema` management command: |
| 79 | + |
| 80 | +```shell |
| 81 | +$ python manage.py generateschema --format openapi > schema.yml |
| 82 | +``` |
| 83 | + |
| 84 | +There's lots of different tooling that you can use for working with OpenAPI |
| 85 | +schemas. One option that we're working on is the [API Star](https://docs.apistar.com/) |
| 86 | +command line tool. |
| 87 | + |
| 88 | +You can use `apistar` to validate your API schema: |
| 89 | + |
| 90 | +```shell |
| 91 | +$ apistar validate --path schema.json --format openapi |
| 92 | +✓ Valid OpenAPI schema. |
| 93 | +``` |
| 94 | + |
| 95 | +Or to build API documentation: |
| 96 | + |
| 97 | +```shell |
| 98 | +$ apistar docs --path schema.json --format openapi |
| 99 | +✓ Documentation built at "build/index.html". |
| 100 | +``` |
| 101 | + |
| 102 | +API Star also includes a [dynamic client library](https://docs.apistar.com/client-library/) |
| 103 | +that uses an API schema to automatically provide a client library interface for making requests. |
| 104 | + |
| 105 | +## Composable permission classes |
| 106 | + |
| 107 | +You can now compose permission classes using the and/or operators, `&` and `|`. |
| 108 | + |
| 109 | +For example... |
| 110 | + |
| 111 | +```python |
| 112 | +permission_classes = [IsAuthenticated & (ReadOnly | IsAdmin)] |
| 113 | +``` |
| 114 | + |
| 115 | +If you're using custom permission classes then make sure that you are subclassing |
| 116 | +from `BasePermission` in order to enable this support. |
| 117 | + |
| 118 | +## ViewSet _Extra Actions_ available in the Browsable API |
| 119 | + |
| 120 | +Following the introduction of the `action` decorator in v3.8, _extra actions_ defined on a ViewSet are now available |
| 121 | +from the Browsable API. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +When defined, a dropdown of "Extra Actions", appropriately filtered to detail/non-detail actions, is displayed. |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Supported Versions |
| 130 | + |
| 131 | +REST framework 3.9 supports Django versions 1.11, 2.0, and 2.1. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Deprecations |
| 136 | + |
| 137 | +### `DjangoObjectPermissionsFilter` moved to third-party package. |
| 138 | + |
| 139 | +The `DjangoObjectPermissionsFilter` class is pending deprecation, will be deprecated in 3.10 and removed entirely in 3.11. |
| 140 | + |
| 141 | +It has been moved to the third-party [`djangorestframework-guardian`](https://github.com/rpkilby/django-rest-framework-guardian) |
| 142 | +package. Please use this instead. |
| 143 | + |
| 144 | +### Router argument/method renamed to use `basename` for consistency. |
| 145 | + |
| 146 | +* The `Router.register` `base_name` argument has been renamed in favor of `basename`. |
| 147 | +* The `Router.get_default_base_name` method has been renamed in favor of `Router.get_default_basename`. [#5990][gh5990] |
| 148 | + |
| 149 | +See [#5990][gh5990]. |
| 150 | + |
| 151 | +[gh5990]: https://github.com/encode/django-rest-framework/pull/5990 |
| 152 | + |
| 153 | +`base_name` and `get_default_base_name()` are pending deprecation. They will be deprecated in 3.10 and removed entirely in 3.11. |
| 154 | + |
| 155 | +### `action` decorator replaces `list_route` and `detail_route` |
| 156 | + |
| 157 | +Both `list_route` and `detail_route` are now deprecated in favour of the single `action` decorator. |
| 158 | +They will be removed entirely in 3.10. |
| 159 | + |
| 160 | +The `action` decorator takes a boolean `detail` argument. |
| 161 | + |
| 162 | +* Replace `detail_route` uses with `@action(detail=True)`. |
| 163 | +* Replace `list_route` uses with `@action(detail=False)`. |
| 164 | + |
| 165 | +### `exclude_from_schema` |
| 166 | + |
| 167 | +Both `APIView.exclude_from_schema` and the `exclude_from_schema` argument to the `@api_view` have now been removed. |
| 168 | + |
| 169 | +For `APIView` you should instead set a `schema = None` attribute on the view class. |
| 170 | + |
| 171 | +For function based views the `@schema` decorator can be used to exclude the view from the schema, by using `@schema(None)`. |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## Minor fixes and improvements |
| 176 | + |
| 177 | +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. |
| 178 | + |
| 179 | + |
| 180 | +## What's next |
| 181 | + |
| 182 | +We're planning to iteratively working towards OpenAPI becoming the standard schema |
| 183 | +representation. This will mean that the `coreapi` dependency will gradually become |
| 184 | +removed, and we'll instead generate the schema directly, rather than building |
| 185 | +a CoreAPI `Document` object. |
| 186 | + |
| 187 | +OpenAPI has clearly become the standard for specifying Web APIs, so there's not |
| 188 | +much value any more in our schema-agnostic document model. Making this change |
| 189 | +will mean that we'll more easily be able to take advantage of the full set of |
| 190 | +OpenAPI functionality. |
| 191 | + |
| 192 | +This will also make a wider range of tooling available. |
| 193 | + |
| 194 | +We'll focus on continuing to develop the [API Star](https://docs.apistar.com/) |
| 195 | +library and client tool into a recommended option for generating API docs, |
| 196 | +validating API schemas, and providing a dynamic client library. |
| 197 | + |
| 198 | +There's also a huge amount of ongoing work on maturing the ASGI landscape, |
| 199 | +with the possibility that some of this work will eventually [feed back into |
| 200 | +Django](https://www.aeracode.org/2018/06/04/django-async-roadmap/). |
| 201 | + |
| 202 | +There will be further work on the [Uvicorn](https://www.uvicorn.org/) |
| 203 | +webserver, as well as lots of functionality planned for the [Starlette](https://www.starlette.io/) |
| 204 | +web framework, which is building a foundational set of tooling for working with |
| 205 | +ASGI. |
| 206 | + |
| 207 | + |
| 208 | +[funding]: funding.md |
| 209 | +[gh5886]: https://github.com/encode/django-rest-framework/issues/5886 |
| 210 | +[gh5705]: https://github.com/encode/django-rest-framework/issues/5705 |
| 211 | +[openapi]: https://www.openapis.org/ |
| 212 | +[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors |
0 commit comments