Debian Codegraph @

docs/community/3.5-announcement.md

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