Debian Codegraph @

docs/community/3.1-announcement.md

raw
1 # Django REST framework 3.1
2
3 The 3.1 release is an intermediate step in the Kickstarter project releases, and includes a range of new functionality.
4
5 Some highlights include:
6
7 * A super-smart cursor pagination scheme.
8 * An improved pagination API, supporting header or in-body pagination styles.
9 * Pagination controls rendering in the browsable API.
10 * Better support for API versioning.
11 * Built-in internationalization support.
12 * Support for Django 1.8's `HStoreField` and `ArrayField`.
13
14 ---
15
16 ## Pagination
17
18 The pagination API has been improved, making it both easier to use, and more powerful.
19
20 A guide to the headline features follows. For full details, see [the pagination documentation][pagination].
21
22 Note that as a result of this work a number of settings keys and generic view attributes are now moved to pending deprecation. Controlling pagination styles is now largely handled by overriding a pagination class and modifying its configuration attributes.
23
24 * The `PAGINATE_BY` settings key will continue to work but is now pending deprecation. The more obviously named `PAGE_SIZE` settings key should now be used instead.
25 * The `PAGINATE_BY_PARAM`, `MAX_PAGINATE_BY` settings keys will continue to work but are now pending deprecation, in favor of setting configuration attributes on the configured pagination class.
26 * The `paginate_by`, `page_query_param`, `paginate_by_param` and `max_paginate_by` generic view attributes will continue to work but are now pending deprecation, in favor of setting configuration attributes on the configured pagination class.
27 * The `pagination_serializer_class` view attribute and `DEFAULT_PAGINATION_SERIALIZER_CLASS` settings key **are no longer valid**. The pagination API does not use serializers to determine the output format, and you'll need to instead override the `get_paginated_response` method on a pagination class in order to specify how the output format is controlled.
28
29 #### New pagination schemes.
30
31 Until now, there has only been a single built-in pagination style in REST framework. We now have page, limit/offset and cursor based schemes included by default.
32
33 The cursor based pagination scheme is particularly smart, and is a better approach for clients iterating through large or frequently changing result sets. The scheme supports paging against non-unique indexes, by using both cursor and limit/offset information. It also allows for both forward and reverse cursor pagination. Much credit goes to David Cramer for [this blog post](https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api) on the subject.
34
35 #### Pagination controls in the browsable API.
36
37 Paginated results now include controls that render directly in the browsable API. If you're using the page or limit/offset style, then you'll see a page based control displayed in the browsable API:
38
39 ![page number based pagination](../img/pages-pagination.png )
40
41 The cursor based pagination renders a more simple style of control:
42
43 ![cursor based pagination](../img/cursor-pagination.png )
44
45 #### Support for header-based pagination.
46
47 The pagination API was previously only able to alter the pagination style in the body of the response. The API now supports being able to write pagination information in response headers, making it possible to use pagination schemes that use the `Link` or `Content-Range` headers.
48
49 For more information, see the [custom pagination styles](../api-guide/pagination.md#custom-pagination-styles) documentation.
50
51 ---
52
53 ## Versioning
54
55 We've made it [easier to build versioned APIs][versioning]. Built-in schemes for versioning include both URL based and Accept header based variations.
56
57 When using a URL based scheme, hyperlinked serializers will resolve relationships to the same API version as used on the incoming request.
58
59 For example, when using `NamespaceVersioning`, and the following hyperlinked serializer:
60
61 class AccountsSerializer(serializer.HyperlinkedModelSerializer):
62 class Meta:
63 model = Accounts
64 fields = ['account_name', 'users']
65
66 The output representation would match the version used on the incoming request. Like so:
67
68 GET http://example.org/v2/accounts/10 # Version 'v2'
69
70 {
71 "account_name": "europa",
72 "users": [
73 "http://example.org/v2/users/12", # Version 'v2'
74 "http://example.org/v2/users/54",
75 "http://example.org/v2/users/87"
76 ]
77 }
78
79 ---
80
81 ## Internationalization
82
83 REST framework now includes a built-in set of translations, and [supports internationalized error responses][internationalization]. This allows you to either change the default language, or to allow clients to specify the language via the `Accept-Language` header.
84
85 You can change the default language by using the standard Django `LANGUAGE_CODE` setting:
86
87 LANGUAGE_CODE = "es-es"
88
89 You can turn on per-request language requests by adding `LocalMiddleware` to your `MIDDLEWARE_CLASSES` setting:
90
91 MIDDLEWARE_CLASSES = [
92 ...
93 'django.middleware.locale.LocaleMiddleware'
94 ]
95
96 When per-request internationalization is enabled, client requests will respect the `Accept-Language` header where possible. For example, let's make a request for an unsupported media type:
97
98 **Request**
99
100 GET /api/users HTTP/1.1
101 Accept: application/xml
102 Accept-Language: es-es
103 Host: example.org
104
105 **Response**
106
107 HTTP/1.0 406 NOT ACCEPTABLE
108
109 {
110 "detail": "No se ha podido satisfacer la solicitud de cabecera de Accept."
111 }
112
113 Note that the structure of the error responses is still the same. We still have a `detail` key in the response. If needed you can modify this behavior too, by using a [custom exception handler][custom-exception-handler].
114
115 We include built-in translations both for standard exception cases, and for serializer validation errors.
116
117 The full list of supported languages can be found on our [Transifex project page](https://www.transifex.com/django-rest-framework-1/django-rest-framework/).
118
119 If you only wish to support a subset of the supported languages, use Django's standard `LANGUAGES` setting:
120
121 LANGUAGES = [
122 ('de', _('German')),
123 ('en', _('English')),
124 ]
125
126 For more details, see the [internationalization documentation][internationalization].
127
128 Many thanks to [Craig Blaszczyk](https://github.com/jakul) for helping push this through.
129
130 ---
131
132 ## New field types
133
134 Django 1.8's new `ArrayField`, `HStoreField` and `UUIDField` are now all fully supported.
135
136 This work also means that we now have both `serializers.DictField()`, and `serializers.ListField()` types, allowing you to express and validate a wider set of representations.
137
138 If you're building a new 1.8 project, then you should probably consider using `UUIDField` as the primary keys for all your models. This style will work automatically with hyperlinked serializers, returning URLs in the following style:
139
140 http://example.org/api/purchases/9b1a433f-e90d-4948-848b-300fdc26365d
141
142 ---
143
144 ## ModelSerializer API
145
146 The serializer redesign in 3.0 did not include any public API for modifying how ModelSerializer classes automatically generate a set of fields from a given mode class. We've now re-introduced an API for this, allowing you to create new ModelSerializer base classes that behave differently, such as using a different default style for relationships.
147
148 For more information, see the documentation on [customizing field mappings][customizing-field-mappings] for ModelSerializer classes.
149
150 ---
151
152 ## Moving packages out of core
153
154 We've now moved a number of packages out of the core of REST framework, and into separately installable packages. If you're currently using these you don't need to worry, you simply need to `pip install` the new packages, and change any import paths.
155
156 We're making this change in order to help distribute the maintenance workload, and keep better focus of the core essentials of the framework.
157
158 The change also means we can be more flexible with which external packages we recommend. For example, the excellently maintained [Django OAuth toolkit](https://github.com/jazzband/django-oauth-toolkit) has now been promoted as our recommended option for integrating OAuth support.
159
160 The following packages are now moved out of core and should be separately installed:
161
162 * OAuth - [djangorestframework-oauth](https://jpadilla.github.io/django-rest-framework-oauth/)
163 * XML - [djangorestframework-xml](https://jpadilla.github.io/django-rest-framework-xml/)
164 * YAML - [djangorestframework-yaml](https://jpadilla.github.io/django-rest-framework-yaml/)
165 * JSONP - [djangorestframework-jsonp](https://jpadilla.github.io/django-rest-framework-jsonp/)
166
167 It's worth reiterating that this change in policy shouldn't mean any work in your codebase other than adding a new requirement and modifying some import paths. For example to install XML rendering, you would now do:
168
169 pip install djangorestframework-xml
170
171 And modify your settings, like so:
172
173 REST_FRAMEWORK = {
174 'DEFAULT_RENDERER_CLASSES': [
175 'rest_framework.renderers.JSONRenderer',
176 'rest_framework.renderers.BrowsableAPIRenderer',
177 'rest_framework_xml.renderers.XMLRenderer'
178 ]
179 }
180
181 Thanks go to the latest member of our maintenance team, [José Padilla](https://github.com/jpadilla/), for handling this work and taking on ownership of these packages.
182
183 ---
184
185 ## Deprecations
186
187 The `request.DATA`, `request.FILES` and `request.QUERY_PARAMS` attributes move from pending deprecation, to deprecated. Use `request.data` and `request.query_params` instead, as discussed in the 3.0 release notes.
188
189 The ModelSerializer Meta options for `write_only_fields`, `view_name` and `lookup_field` are also moved from pending deprecation, to deprecated. Use `extra_kwargs` instead, as discussed in the 3.0 release notes.
190
191 All these attributes and options will still work in 3.1, but their usage will raise a warning. They will be fully removed in 3.2.
192
193 ---
194
195 ## What's next?
196
197 The next focus will be on HTML renderings of API output and will include:
198
199 * HTML form rendering of serializers.
200 * Filtering controls built-in to the browsable API.
201 * An alternative admin-style interface.
202
203 This will either be made as a single 3.2 release, or split across two separate releases, with the HTML forms and filter controls coming in 3.2, and the admin-style interface coming in a 3.3 release.
204
205 [custom-exception-handler]: ../api-guide/exceptions.md#custom-exception-handling
206 [pagination]: ../api-guide/pagination.md
207 [versioning]: ../api-guide/versioning.md
208 [internationalization]: ../topics/internationalization.md
209 [customizing-field-mappings]: ../api-guide/serializers.md#customizing-field-mappings