| 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.5 |
| 21 |
|
| 22 |
The 3.5 release is the second in a planned series that is addressing schema |
| 23 |
generation, hypermedia support, API client libraries, and finally realtime support. |
| 24 |
|
| 25 |
--- |
| 26 |
|
| 27 |
## Funding |
| 28 |
|
| 29 |
The 3.5 release would not have been possible without our [collaborative funding model][funding]. |
| 30 |
If you use REST framework commercially and would like to see this work continue, |
| 31 |
we strongly encourage you to invest in its continued development by |
| 32 |
**[signing up for a paid plan][funding]**. |
| 33 |
|
| 34 |
*Many thanks to all our [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), and [Machinalis](https://www.machinalis.com/#services).* |
| 35 |
|
| 36 |
--- |
| 37 |
|
| 38 |
## Improved schema generation |
| 39 |
|
| 40 |
Docstrings on views are now pulled through into schema definitions, allowing |
| 41 |
you to [use the schema definition to document your API][schema-docs]. |
| 42 |
|
| 43 |
There is now also a shortcut function, `get_schema_view()`, which makes it easier to |
| 44 |
[adding schema views][schema-view] to your API. |
| 45 |
|
| 46 |
For example, to include a swagger schema to your API, you would do the following: |
| 47 |
|
| 48 |
* Run `pip install django-rest-swagger`. |
| 49 |
|
| 50 |
* Add `'rest_framework_swagger'` to your `INSTALLED_APPS` setting. |
| 51 |
|
| 52 |
* Include the schema view in your URL conf: |
| 53 |
|
| 54 |
```py |
| 55 |
from rest_framework.schemas import get_schema_view |
| 56 |
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer |
| 57 |
|
| 58 |
schema_view = get_schema_view( |
| 59 |
title="Example API", renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer] |
| 60 |
) |
| 61 |
|
| 62 |
urlpatterns = [path("swagger/", schema_view), ...] |
| 63 |
``` |
| 64 |
|
| 65 |
There have been a large number of fixes to the schema generation. These should |
| 66 |
resolve issues for anyone using the latest version of the `django-rest-swagger` |
| 67 |
package. |
| 68 |
|
| 69 |
Some of these changes do affect the resulting schema structure, |
| 70 |
so if you're already using schema generation you should make sure to review |
| 71 |
[the deprecation notes](#deprecations), particularly if you're currently using |
| 72 |
a dynamic client library to interact with your API. |
| 73 |
|
| 74 |
Finally, we're also now exposing the schema generation as a |
| 75 |
[publicly documented API][schema-generation-api], allowing you to more easily |
| 76 |
override the behavior. |
| 77 |
|
| 78 |
## Requests test client |
| 79 |
|
| 80 |
You can now test your project using the `requests` library. |
| 81 |
|
| 82 |
This exposes exactly the same interface as if you were using a standard |
| 83 |
requests session instance. |
| 84 |
|
| 85 |
client = RequestsClient() |
| 86 |
response = client.get('http://testserver/users/') |
| 87 |
assert response.status_code == 200 |
| 88 |
|
| 89 |
Rather than sending any HTTP requests to the network, this interface will |
| 90 |
coerce all outgoing requests into WSGI, and call into your application directly. |
| 91 |
|
| 92 |
## Core API client |
| 93 |
|
| 94 |
You can also now test your project by interacting with it using the `coreapi` |
| 95 |
client library. |
| 96 |
|
| 97 |
# Fetch the API schema |
| 98 |
client = CoreAPIClient() |
| 99 |
schema = client.get('http://testserver/schema/') |
| 100 |
|
| 101 |
# Create a new organization |
| 102 |
params = {'name': 'MegaCorp', 'status': 'active'} |
| 103 |
client.action(schema, ['organizations', 'create'], params) |
| 104 |
|
| 105 |
# Ensure that the organization exists in the listing |
| 106 |
data = client.action(schema, ['organizations', 'list']) |
| 107 |
assert(len(data) == 1) |
| 108 |
assert(data == [{'name': 'MegaCorp', 'status': 'active'}]) |
| 109 |
|
| 110 |
Again, this will call directly into the application using the WSGI interface, |
| 111 |
rather than making actual network calls. |
| 112 |
|
| 113 |
This is a good option if you are planning for clients to mainly interact with |
| 114 |
your API using the `coreapi` client library, or some other auto-generated client. |
| 115 |
|
| 116 |
## Live tests |
| 117 |
|
| 118 |
One interesting aspect of both the `requests` client and the `coreapi` client |
| 119 |
is that they allow you to write tests in such a way that they can also be made |
| 120 |
to run against a live service. |
| 121 |
|
| 122 |
By switching the WSGI based client instances to actual instances of `requests.Session` |
| 123 |
or `coreapi.Client` you can have the test cases make actual network calls. |
| 124 |
|
| 125 |
Being able to write test cases that can exercise your staging or production |
| 126 |
environment is a powerful tool. However in order to do this, you'll need to pay |
| 127 |
close attention to how you handle setup and teardown to ensure a strict isolation |
| 128 |
of test data from other live or staging data. |
| 129 |
|
| 130 |
## RAML support |
| 131 |
|
| 132 |
We now have preliminary support for [RAML documentation generation][django-rest-raml]. |
| 133 |
|
| 134 |
![RAML Example][raml-image] |
| 135 |
|
| 136 |
Further work on the encoding and documentation generation is planned, in order to |
| 137 |
make features such as the 'Try it now' support available at a later date. |
| 138 |
|
| 139 |
This work also now means that you can use the Core API client libraries to interact |
| 140 |
with APIs that expose a RAML specification. The [RAML codec][raml-codec] gives some examples of |
| 141 |
interacting with the Spotify API in this way. |
| 142 |
|
| 143 |
## Validation codes |
| 144 |
|
| 145 |
Exceptions raised by REST framework now include short code identifiers. |
| 146 |
When used together with our customizable error handling, this now allows you to |
| 147 |
modify the style of API error messages. |
| 148 |
|
| 149 |
As an example, this allows for the following style of error responses: |
| 150 |
|
| 151 |
{ |
| 152 |
"message": "You do not have permission to perform this action.", |
| 153 |
"code": "permission_denied" |
| 154 |
} |
| 155 |
|
| 156 |
This is particularly useful with validation errors, which use appropriate |
| 157 |
codes to identify differing kinds of failure... |
| 158 |
|
| 159 |
{ |
| 160 |
"name": {"message": "This field is required.", "code": "required"}, |
| 161 |
"age": {"message": "A valid integer is required.", "code": "invalid"} |
| 162 |
} |
| 163 |
|
| 164 |
## Client upload & download support |
| 165 |
|
| 166 |
The Python `coreapi` client library and the Core API command line tool both |
| 167 |
now fully support file [uploads][uploads] and [downloads][downloads]. |
| 168 |
|
| 169 |
--- |
| 170 |
|
| 171 |
## Deprecations |
| 172 |
|
| 173 |
### Generating schemas from Router |
| 174 |
|
| 175 |
The router arguments for generating a schema view, such as `schema_title`, |
| 176 |
are now pending deprecation. |
| 177 |
|
| 178 |
Instead of using `DefaultRouter(schema_title='Example API')`, you should use |
| 179 |
the `get_schema_view()` function, and include the view in your URL conf. |
| 180 |
|
| 181 |
Make sure to include the view before your router urls. For example: |
| 182 |
|
| 183 |
from rest_framework.schemas import get_schema_view |
| 184 |
from my_project.routers import router |
| 185 |
|
| 186 |
schema_view = get_schema_view(title='Example API') |
| 187 |
|
| 188 |
urlpatterns = [ |
| 189 |
path('', schema_view), |
| 190 |
path('', include(router.urls)), |
| 191 |
] |
| 192 |
|
| 193 |
### Schema path representations |
| 194 |
|
| 195 |
The `'pk'` identifier in schema paths is now mapped onto the actually model field |
| 196 |
name by default. This will typically be `'id'`. |
| 197 |
|
| 198 |
This gives a better external representation for schemas, with less implementation |
| 199 |
detail being exposed. It also reflects the behavior of using a ModelSerializer |
| 200 |
class with `fields = '__all__'`. |
| 201 |
|
| 202 |
You can revert to the previous behavior by setting `'SCHEMA_COERCE_PATH_PK': False` |
| 203 |
in the REST framework settings. |
| 204 |
|
| 205 |
### Schema action name representations |
| 206 |
|
| 207 |
The internal `retrieve()` and `destroy()` method names are now coerced to an |
| 208 |
external representation of `read` and `delete`. |
| 209 |
|
| 210 |
You can revert to the previous behavior by setting `'SCHEMA_COERCE_METHOD_NAMES': {}` |
| 211 |
in the REST framework settings. |
| 212 |
|
| 213 |
### DjangoFilterBackend |
| 214 |
|
| 215 |
The functionality of the built-in `DjangoFilterBackend` is now completely |
| 216 |
included by the `django-filter` package. |
| 217 |
|
| 218 |
You should change your imports and REST framework filter settings as follows: |
| 219 |
|
| 220 |
* `rest_framework.filters.DjangoFilterBackend` becomes `django_filters.rest_framework.DjangoFilterBackend`. |
| 221 |
* `rest_framework.filters.FilterSet` becomes `django_filters.rest_framework.FilterSet`. |
| 222 |
|
| 223 |
The existing imports will continue to work but are now pending deprecation. |
| 224 |
|
| 225 |
### CoreJSON media type |
| 226 |
|
| 227 |
The media type for `CoreJSON` is now `application/json+coreapi`, rather than |
| 228 |
the previous `application/vnd.json+coreapi`. This brings it more into line with |
| 229 |
other custom media types, such as those used by Swagger and RAML. |
| 230 |
|
| 231 |
The clients currently accept either media type. The old style-media type will |
| 232 |
be deprecated at a later date. |
| 233 |
|
| 234 |
### ModelSerializer 'fields' and 'exclude' |
| 235 |
|
| 236 |
ModelSerializer and HyperlinkedModelSerializer must include either a fields |
| 237 |
option, or an exclude option. The `fields = '__all__'` shortcut may be used to |
| 238 |
explicitly include all fields. |
| 239 |
|
| 240 |
Failing to set either `fields` or `exclude` raised a pending deprecation warning |
| 241 |
in version 3.3 and raised a deprecation warning in 3.4. Its usage is now mandatory. |
| 242 |
|
| 243 |
--- |
| 244 |
|
| 245 |
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors |
| 246 |
[funding]: https://opencollective.com/django-rest-framework |
| 247 |
[uploads]: https://core-api.github.io/python-client/api-guide/utils/#file |
| 248 |
[downloads]: https://core-api.github.io/python-client/api-guide/codecs/#downloadcodec |
| 249 |
[schema-generation-api]: ../api-guide/schemas.md#schemagenerator |
| 250 |
[schema-docs]: ../api-guide/schemas.md#schemas-as-documentation |
| 251 |
[schema-view]: ../api-guide/schemas.md#get_schema_view |
| 252 |
[django-rest-raml]: https://github.com/encode/django-rest-raml |
| 253 |
[raml-image]: ../img/raml.png |
| 254 |
[raml-codec]: https://github.com/core-api/python-raml-codec |