django-rest-framework/community/3.5-announcement/index.html
2023-11-29 14:16:17 +00:00

717 lines
30 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>3.5 Announcement - Django REST framework</title>
<link href="../../img/favicon.ico" rel="icon" type="image/x-icon">
<link rel="canonical" href="https://www.django-rest-framework.org/community/3.5-announcement/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Django, API, REST, 3.5 Announcement">
<meta name="author" content="Tom Christie">
<!-- Le styles -->
<link href="../../css/prettify.css" rel="stylesheet">
<link href="../../css/bootstrap.css" rel="stylesheet">
<link href="../../css/bootstrap-responsive.css" rel="stylesheet">
<link href="../../css/default.css" rel="stylesheet">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18852272-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<style>
#sidebarInclude img {
margin-bottom: 10px;
}
#sidebarInclude a.promo {
color: black;
}
@media (max-width: 767px) {
div.promo {
display: none;
}
}
</style>
</head>
<body onload="prettyPrint()" class="-page">
<div class="wrapper">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="repo-link btn btn-primary btn-small" href="https://github.com/encode/django-rest-framework/tree/master">GitHub</a>
<a class="repo-link btn btn-inverse btn-small " rel="next" href="../3.4-announcement/">
Next <i class="icon-arrow-right icon-white"></i>
</a>
<a class="repo-link btn btn-inverse btn-small " rel="prev" href="../3.6-announcement/">
<i class="icon-arrow-left icon-white"></i> Previous
</a>
<a id="search_modal_show" class="repo-link btn btn-inverse btn-small" href="#mkdocs_search_modal" data-toggle="modal" data-target="#mkdocs_search_modal"><i class="icon-search icon-white"></i> Search</a>
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="https://www.django-rest-framework.org/">Django REST framework</a>
<div class="nav-collapse collapse">
<!-- Main navigation -->
<ul class="nav navbar-nav">
<li >
<a href="../..">Home</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Tutorial <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
<a href="../../tutorial/quickstart/">Quickstart</a>
</li>
<li >
<a href="../../tutorial/1-serialization/">1 - Serialization</a>
</li>
<li >
<a href="../../tutorial/2-requests-and-responses/">2 - Requests and responses</a>
</li>
<li >
<a href="../../tutorial/3-class-based-views/">3 - Class based views</a>
</li>
<li >
<a href="../../tutorial/4-authentication-and-permissions/">4 - Authentication and permissions</a>
</li>
<li >
<a href="../../tutorial/5-relationships-and-hyperlinked-apis/">5 - Relationships and hyperlinked APIs</a>
</li>
<li >
<a href="../../tutorial/6-viewsets-and-routers/">6 - Viewsets and routers</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API Guide <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
<a href="../../api-guide/requests/">Requests</a>
</li>
<li >
<a href="../../api-guide/responses/">Responses</a>
</li>
<li >
<a href="../../api-guide/views/">Views</a>
</li>
<li >
<a href="../../api-guide/generic-views/">Generic views</a>
</li>
<li >
<a href="../../api-guide/viewsets/">Viewsets</a>
</li>
<li >
<a href="../../api-guide/routers/">Routers</a>
</li>
<li >
<a href="../../api-guide/parsers/">Parsers</a>
</li>
<li >
<a href="../../api-guide/renderers/">Renderers</a>
</li>
<li >
<a href="../../api-guide/serializers/">Serializers</a>
</li>
<li >
<a href="../../api-guide/fields/">Serializer fields</a>
</li>
<li >
<a href="../../api-guide/relations/">Serializer relations</a>
</li>
<li >
<a href="../../api-guide/validators/">Validators</a>
</li>
<li >
<a href="../../api-guide/authentication/">Authentication</a>
</li>
<li >
<a href="../../api-guide/permissions/">Permissions</a>
</li>
<li >
<a href="../../api-guide/caching/">Caching</a>
</li>
<li >
<a href="../../api-guide/throttling/">Throttling</a>
</li>
<li >
<a href="../../api-guide/filtering/">Filtering</a>
</li>
<li >
<a href="../../api-guide/pagination/">Pagination</a>
</li>
<li >
<a href="../../api-guide/versioning/">Versioning</a>
</li>
<li >
<a href="../../api-guide/content-negotiation/">Content negotiation</a>
</li>
<li >
<a href="../../api-guide/metadata/">Metadata</a>
</li>
<li >
<a href="../../api-guide/schemas/">Schemas</a>
</li>
<li >
<a href="../../api-guide/format-suffixes/">Format suffixes</a>
</li>
<li >
<a href="../../api-guide/reverse/">Returning URLs</a>
</li>
<li >
<a href="../../api-guide/exceptions/">Exceptions</a>
</li>
<li >
<a href="../../api-guide/status-codes/">Status codes</a>
</li>
<li >
<a href="../../api-guide/testing/">Testing</a>
</li>
<li >
<a href="../../api-guide/settings/">Settings</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topics <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
<a href="../../topics/documenting-your-api/">Documenting your API</a>
</li>
<li >
<a href="../../topics/internationalization/">Internationalization</a>
</li>
<li >
<a href="../../topics/ajax-csrf-cors/">AJAX, CSRF & CORS</a>
</li>
<li >
<a href="../../topics/html-and-forms/">HTML & Forms</a>
</li>
<li >
<a href="../../topics/browser-enhancements/">Browser Enhancements</a>
</li>
<li >
<a href="../../topics/browsable-api/">The Browsable API</a>
</li>
<li >
<a href="../../topics/rest-hypermedia-hateoas/">REST, Hypermedia & HATEOAS</a>
</li>
</ul>
</li>
<li class="dropdown active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Community <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
<a href="../tutorials-and-resources/">Tutorials and Resources</a>
</li>
<li >
<a href="../third-party-packages/">Third Party Packages</a>
</li>
<li >
<a href="../contributing/">Contributing to REST framework</a>
</li>
<li >
<a href="../project-management/">Project management</a>
</li>
<li >
<a href="../release-notes/">Release Notes</a>
</li>
<li >
<a href="../3.14-announcement/">3.14 Announcement</a>
</li>
<li >
<a href="../3.13-announcement/">3.13 Announcement</a>
</li>
<li >
<a href="../3.12-announcement/">3.12 Announcement</a>
</li>
<li >
<a href="../3.11-announcement/">3.11 Announcement</a>
</li>
<li >
<a href="../3.10-announcement/">3.10 Announcement</a>
</li>
<li >
<a href="../3.9-announcement/">3.9 Announcement</a>
</li>
<li >
<a href="../3.8-announcement/">3.8 Announcement</a>
</li>
<li >
<a href="../3.7-announcement/">3.7 Announcement</a>
</li>
<li >
<a href="../3.6-announcement/">3.6 Announcement</a>
</li>
<li class="active" >
<a href="./">3.5 Announcement</a>
</li>
<li >
<a href="../3.4-announcement/">3.4 Announcement</a>
</li>
<li >
<a href="../3.3-announcement/">3.3 Announcement</a>
</li>
<li >
<a href="../3.2-announcement/">3.2 Announcement</a>
</li>
<li >
<a href="../3.1-announcement/">3.1 Announcement</a>
</li>
<li >
<a href="../3.0-announcement/">3.0 Announcement</a>
</li>
<li >
<a href="../kickstarter-announcement/">Kickstarter Announcement</a>
</li>
<li >
<a href="../mozilla-grant/">Mozilla Grant</a>
</li>
<li >
<a href="../funding/">Funding</a>
</li>
<li >
<a href="../jobs/">Jobs</a>
</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
<div class="body-content">
<div class="container-fluid">
<!-- Search Modal -->
<div id="mkdocs_search_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3 id="myModalLabel">Documentation search</h3>
</div>
<div class="modal-body">
<form role="form" autocomplete="off">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search..." id="mkdocs-search-query">
</div>
</form>
<div id="mkdocs-search-results"></div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<div id="table-of-contents">
<ul class="nav nav-list side-nav well sidebar-nav-fixed">
<li class="main">
<a href="#django-rest-framework-35">Django REST framework 3.5</a>
</li>
<li>
<a href="#funding">Funding</a>
</li>
<li>
<a href="#improved-schema-generation">Improved schema generation</a>
</li>
<li>
<a href="#requests-test-client">Requests test client</a>
</li>
<li>
<a href="#core-api-client">Core API client</a>
</li>
<li>
<a href="#live-tests">Live tests</a>
</li>
<li>
<a href="#raml-support">RAML support</a>
</li>
<li>
<a href="#validation-codes">Validation codes</a>
</li>
<li>
<a href="#client-upload-download-support">Client upload &amp; download support</a>
</li>
<li>
<a href="#deprecations">Deprecations</a>
</li>
<div class="promo">
<hr/>
<div id="sidebarInclude">
</div>
</ul>
</div>
</div>
<div id="main-content" class="span9">
<style>
.promo li a {
float: left;
width: 130px;
height: 20px;
text-align: center;
margin: 10px 30px;
padding: 150px 0 0 0;
background-position: 0 50%;
background-size: 130px auto;
background-repeat: no-repeat;
font-size: 120%;
color: black;
}
.promo li {
list-style: none;
}
</style>
<h1 id="django-rest-framework-35"><a class="toclink" href="#django-rest-framework-35">Django REST framework 3.5</a></h1>
<p>The 3.5 release is the second in a planned series that is addressing schema
generation, hypermedia support, API client libraries, and finally realtime support.</p>
<hr />
<h2 id="funding"><a class="toclink" href="#funding">Funding</a></h2>
<p>The 3.5 release would not have been possible without our <a href="../funding/">collaborative funding model</a>.
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
<strong><a href="../funding/">signing up for a paid&nbsp;plan</a></strong>.</p>
<ul class="premium-promo promo">
<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>
<li><a href="https://sentry.io/welcome/" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/sentry130.png)">Sentry</a></li>
<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>
<li><a href="https://www.machinalis.com/#services" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/Machinalis130.png)">Machinalis</a></li>
</ul>
<div style="clear: both; padding-bottom: 20px;"></div>
<p><em>Many thanks to all our <a href="https://fund.django-rest-framework.org/topics/funding/#our-sponsors">sponsors</a>, and in particular to our premium backers, <a href="https://www.rover.com/careers/">Rover</a>, <a href="https://sentry.io/welcome/">Sentry</a>, <a href="https://getstream.io/?utm_source=drf&amp;utm_medium=banner&amp;utm_campaign=drf">Stream</a>, and <a href="https://www.machinalis.com/#services">Machinalis</a>.</em></p>
<hr />
<h2 id="improved-schema-generation"><a class="toclink" href="#improved-schema-generation">Improved schema generation</a></h2>
<p>Docstrings on views are now pulled through into schema definitions, allowing
you to <a href="../api-guide/schemas/#schemas-as-documentation">use the schema definition to document your&nbsp;API</a>.</p>
<p>There is now also a shortcut function, <code>get_schema_view()</code>, which makes it easier to
<a href="../api-guide/schemas/#the-get_schema_view-shortcut">adding schema views</a> to your API.</p>
<p>For example, to include a swagger schema to your API, you would do the following:</p>
<ul>
<li>
<p>Run <code>pip install django-rest-swagger</code>.</p>
</li>
<li>
<p>Add <code>'rest_framework_swagger'</code> to your <code>INSTALLED_APPS</code> setting.</p>
</li>
<li>
<p>Include the schema view in your URL conf:</p>
</li>
</ul>
<pre><code class="language-py">from rest_framework.schemas import get_schema_view
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer
schema_view = get_schema_view(
title='Example API',
renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer]
)
urlpatterns = [
path('swagger/', schema_view),
...
]
</code></pre>
<p>There have been a large number of fixes to the schema generation. These should
resolve issues for anyone using the latest version of the <code>django-rest-swagger</code>
package.</p>
<p>Some of these changes do affect the resulting schema structure,
so if you're already using schema generation you should make sure to review
<a href="#deprecations">the deprecation notes</a>, particularly if you're currently using
a dynamic client library to interact with your API.</p>
<p>Finally, we're also now exposing the schema generation as a
<a href="../api-guide/schemas/#schemagenerator">publicly documented API</a>, allowing you to more easily
override the behaviour.</p>
<h2 id="requests-test-client"><a class="toclink" href="#requests-test-client">Requests test client</a></h2>
<p>You can now test your project using the <code>requests</code> library.</p>
<p>This exposes exactly the same interface as if you were using a standard
requests session instance.</p>
<pre><code>client = RequestsClient()
response = client.get('http://testserver/users/')
assert response.status_code == 200
</code></pre>
<p>Rather than sending any HTTP requests to the network, this interface will
coerce all outgoing requests into WSGI, and call into your application directly.</p>
<h2 id="core-api-client"><a class="toclink" href="#core-api-client">Core API client</a></h2>
<p>You can also now test your project by interacting with it using the <code>coreapi</code>
client library.</p>
<pre><code># Fetch the API schema
client = CoreAPIClient()
schema = client.get('http://testserver/schema/')
# Create a new organisation
params = {'name': 'MegaCorp', 'status': 'active'}
client.action(schema, ['organisations', 'create'], params)
# Ensure that the organisation exists in the listing
data = client.action(schema, ['organisations', 'list'])
assert(len(data) == 1)
assert(data == [{'name': 'MegaCorp', 'status': 'active'}])
</code></pre>
<p>Again, this will call directly into the application using the WSGI interface,
rather than making actual network calls.</p>
<p>This is a good option if you are planning for clients to mainly interact with
your API using the <code>coreapi</code> client library, or some other auto-generated client.</p>
<h2 id="live-tests"><a class="toclink" href="#live-tests">Live tests</a></h2>
<p>One interesting aspect of both the <code>requests</code> client and the <code>coreapi</code> client
is that they allow you to write tests in such a way that they can also be made
to run against a live service.</p>
<p>By switching the WSGI based client instances to actual instances of <code>requests.Session</code>
or <code>coreapi.Client</code> you can have the test cases make actual network calls.</p>
<p>Being able to write test cases that can exercise your staging or production
environment is a powerful tool. However in order to do this, you'll need to pay
close attention to how you handle setup and teardown to ensure a strict isolation
of test data from other live or staging data.</p>
<h2 id="raml-support"><a class="toclink" href="#raml-support">RAML support</a></h2>
<p>We now have preliminary support for <a href="https://github.com/encode/django-rest-raml">RAML documentation generation</a>.</p>
<p><img alt="RAML Example" src="../../img/raml.png" /></p>
<p>Further work on the encoding and documentation generation is planned, in order to
make features such as the 'Try it now' support available at a later date.</p>
<p>This work also now means that you can use the Core API client libraries to interact
with APIs that expose a RAML specification. The <a href="https://github.com/core-api/python-raml-codec">RAML codec</a> gives some examples of
interacting with the Spotify API in this way.</p>
<h2 id="validation-codes"><a class="toclink" href="#validation-codes">Validation codes</a></h2>
<p>Exceptions raised by REST framework now include short code identifiers.
When used together with our customizable error handling, this now allows you to
modify the style of API error messages.</p>
<p>As an example, this allows for the following style of error responses:</p>
<pre><code>{
"message": "You do not have permission to perform this action.",
"code": "permission_denied"
}
</code></pre>
<p>This is particularly useful with validation errors, which use appropriate
codes to identify differing kinds of failure...</p>
<pre><code>{
"name": {"message": "This field is required.", "code": "required"},
"age": {"message": "A valid integer is required.", "code": "invalid"}
}
</code></pre>
<h2 id="client-upload-download-support"><a class="toclink" href="#client-upload-download-support">Client upload &amp; download support</a></h2>
<p>The Python <code>coreapi</code> client library and the Core API command line tool both
now fully support file <a href="https://core-api.github.io/python-client/api-guide/utils/#file">uploads</a> and <a href="https://core-api.github.io/python-client/api-guide/codecs/#downloadcodec">downloads</a>.</p>
<hr />
<h2 id="deprecations"><a class="toclink" href="#deprecations">Deprecations</a></h2>
<h3 id="generating-schemas-from-router"><a class="toclink" href="#generating-schemas-from-router">Generating schemas from Router</a></h3>
<p>The router arguments for generating a schema view, such as <code>schema_title</code>,
are now pending deprecation.</p>
<p>Instead of using <code>DefaultRouter(schema_title='Example API')</code>, you should use
the <code>get_schema_view()</code> function, and include the view in your URL conf.</p>
<p>Make sure to include the view before your router urls. For example:</p>
<pre><code>from rest_framework.schemas import get_schema_view
from my_project.routers import router
schema_view = get_schema_view(title='Example API')
urlpatterns = [
path('', schema_view),
path('', include(router.urls)),
]
</code></pre>
<h3 id="schema-path-representations"><a class="toclink" href="#schema-path-representations">Schema path representations</a></h3>
<p>The <code>'pk'</code> identifier in schema paths is now mapped onto the actually model field
name by default. This will typically be <code>'id'</code>.</p>
<p>This gives a better external representation for schemas, with less implementation
detail being exposed. It also reflects the behaviour of using a ModelSerializer
class with <code>fields = '__all__'</code>.</p>
<p>You can revert to the previous behaviour by setting <code>'SCHEMA_COERCE_PATH_PK': False</code>
in the REST framework settings.</p>
<h3 id="schema-action-name-representations"><a class="toclink" href="#schema-action-name-representations">Schema action name representations</a></h3>
<p>The internal <code>retrieve()</code> and <code>destroy()</code> method names are now coerced to an
external representation of <code>read</code> and <code>delete</code>.</p>
<p>You can revert to the previous behaviour by setting <code>'SCHEMA_COERCE_METHOD_NAMES': {}</code>
in the REST framework settings.</p>
<h3 id="djangofilterbackend"><a class="toclink" href="#djangofilterbackend">DjangoFilterBackend</a></h3>
<p>The functionality of the built-in <code>DjangoFilterBackend</code> is now completely
included by the <code>django-filter</code> package.</p>
<p>You should change your imports and REST framework filter settings as follows:</p>
<ul>
<li><code>rest_framework.filters.DjangoFilterBackend</code> becomes <code>django_filters.rest_framework.DjangoFilterBackend</code>.</li>
<li><code>rest_framework.filters.FilterSet</code> becomes <code>django_filters.rest_framework.FilterSet</code>.</li>
</ul>
<p>The existing imports will continue to work but are now pending deprecation.</p>
<h3 id="corejson-media-type"><a class="toclink" href="#corejson-media-type">CoreJSON media type</a></h3>
<p>The media type for <code>CoreJSON</code> is now <code>application/json+coreapi</code>, rather than
the previous <code>application/vnd.json+coreapi</code>. This brings it more into line with
other custom media types, such as those used by Swagger and RAML.</p>
<p>The clients currently accept either media type. The old style-media type will
be deprecated at a later date.</p>
<h3 id="modelserializer-fields-and-exclude"><a class="toclink" href="#modelserializer-fields-and-exclude">ModelSerializer 'fields' and 'exclude'</a></h3>
<p>ModelSerializer and HyperlinkedModelSerializer must include either a fields
option, or an exclude option. The <code>fields = '__all__'</code> shortcut may be used to
explicitly include all fields.</p>
<p>Failing to set either <code>fields</code> or <code>exclude</code> raised a pending deprecation warning
in version 3.3 and raised a deprecation warning in 3.4. Its usage is now mandatory.</p>
<hr />
</div> <!--/span-->
</div> <!--/row-->
</div> <!--/.fluid-container-->
</div> <!--/.body content-->
<div id="push"></div>
</div> <!--/.wrapper -->
<footer class="span12">
<p>Documentation built with <a href="http://www.mkdocs.org/">MkDocs</a>.
</p>
</footer>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script async src="https://fund.django-rest-framework.org/sidebar_include.js"></script>
<script src="../../js/jquery-1.8.1-min.js"></script>
<script src="../../js/prettify-1.0.js"></script>
<script src="../../js/bootstrap-2.1.1-min.js"></script>
<script src="../../js/theme.js"></script>
<script>var base_url = '../..';</script>
<script src="../../search/main.js" defer></script>
<script>
var shiftWindow = function() {
scrollBy(0, -50)
};
if (location.hash) shiftWindow();
window.addEventListener("hashchange", shiftWindow);
$('.dropdown-menu').on('click touchstart', function(event) {
event.stopPropagation();
});
// Dynamically force sidenav/dropdown to no higher than browser window
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
$(function() {
$(window).resize(function() {
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
});
});
</script>
</body>
</html>