- Language: en
- Documentation version: 3.12
- Generic views
- Serializer fields
- Serializer relations
- Content negotiation
- Format suffixes
- Returning URLs
- Status codes
- Tutorials and Resources
- Third Party Packages
- Contributing to REST framework
- Project management
- Release Notes
- 3.12 Announcement
- 3.11 Announcement
- 3.10 Announcement
- 3.9 Announcement
- 3.8 Announcement
- 3.7 Announcement
- 3.6 Announcement
- 3.5 Announcement
- 3.4 Announcement
- 3.3 Announcement
- 3.2 Announcement
- 3.1 Announcement
- 3.0 Announcement
- Kickstarter Announcement
- Mozilla Grant
Django REST framework 3.9¶
The 3.9 release gives access to extra actions in the Browsable API, introduces composable permissions and built-in OpenAPI schema support. (Formerly known as Swagger)
- :raw-html-m2r:`<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/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> <li><a href=”https://auklet.io” style=”background-image: url(https://fund-rest-framework.s3.amazonaws.com/auklet-new.png)”>Auklet</a></li> <li><a href=”https://rollbar.com” style=”background-image: url(https://fund-rest-framework.s3.amazonaws.com/rollbar2.png)”>Rollbar</a></li> <li><a href=”https://cadre.com” style=”background-image: url(https://fund-rest-framework.s3.amazonaws.com/cadre.png)”>Cadre</a></li> <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> <li><a href=”https://hubs.ly/H0f30Lf0” style=”background-image: url(https://fund-rest-framework.s3.amazonaws.com/kloudless.png)”>Kloudless</a></li>
Many thanks to all our :doc:`wonderful sponsors <https://fund.django-rest-framework.org/topics/funding/#our-sponsors>`, and in particular to our premium backers, :doc:`Rover <https://www.rover.com/careers/>`, :doc:`Sentry <https://sentry.io/welcome/>`, `Stream <https://getstream.io/?utm_source=drf&utm_medium=banner&utm_campaign=drf>`_, :doc:`Auklet <https://auklet.io/>`, :doc:`Rollbar <https://rollbar.com>`, :doc:`Cadre <https://cadre.com>`, `Load Impact <https://loadimpact.com/?utm_campaign=Sponsorship%20links&utm_source=drf&utm_medium=drf>`_, and :doc:`Kloudless <https://hubs.ly/H0f30Lf0>`.
Built-in OpenAPI schema support¶
REST framework now has a first-pass at directly including OpenAPI schema support. (Formerly known as Swagger)
There are now
JSONOpenAPIRendererclasses that deal with encoding
coreapi.Documentinstances into OpenAPI YAML or OpenAPI JSON.
get_schema_view(...)method now defaults to OpenAPI YAML, with CoreJSON as a secondary option if it is selected via HTTP content negotiation.
There is a new management command
generateschema, which you can use to dump the schema into your repository.
Here’s an example of adding an OpenAPI schema to the URL conf:
from rest_framework.schemas import get_schema_view from rest_framework.renderers import JSONOpenAPIRenderer from django.urls import path schema_view = get_schema_view( title='Server Monitoring API', url='https://www.example.org/api/', renderer_classes=[JSONOpenAPIRenderer] ) urlpatterns = [ path('schema.json', schema_view), ... ]
And here’s how you can use the
generateschema management command:
$ python manage.py generateschema --format openapi > schema.yml
There’s lots of different tooling that you can use for working with OpenAPI schemas. One option that we’re working on is the API Star command line tool.
You can use
apistar to validate your API schema:
$ apistar validate --path schema.json --format openapi ✓ Valid OpenAPI schema.
Or to build API documentation:
$ apistar docs --path schema.json --format openapi ✓ Documentation built at "build/index.html".
API Star also includes a dynamic client library that uses an API schema to automatically provide a client library interface for making requests.
Composable permission classes¶
You can now compose permission classes using the and/or operators,
permission_classes = [IsAuthenticated & (ReadOnly | IsAdmin)]
If you’re using custom permission classes then make sure that you are subclassing
BasePermission in order to enable this support.
ViewSet Extra Actions available in the Browsable API¶
Following the introduction of the
action decorator in v3.8, extra actions defined on a ViewSet are now available
from the Browsable API.
When defined, a dropdown of “Extra Actions”, appropriately filtered to detail/non-detail actions, is displayed.
REST framework 3.9 supports Django versions 1.11, 2.0, and 2.1.
DjangoObjectPermissionsFilter moved to third-party package.¶
DjangoObjectPermissionsFilter class is pending deprecation, will be deprecated in 3.10 and removed entirely in 3.11.
It has been moved to the third-party ``djangorestframework-guardian:doc:` <https://github.com/rpkilby/django-rest-framework-guardian>` package. Please use this instead.
Router argument/method renamed to use
basename for consistency.¶
base_nameargument has been renamed in favor of
Router.get_default_base_namemethod has been renamed in favor of
get_default_base_name() are pending deprecation. They will be deprecated in 3.10 and removed entirely in 3.11.
action decorator replaces
detail_route are now deprecated in favour of the single
They will be removed entirely in 3.10.
action decorator takes a boolean
APIView.exclude_from_schema and the
exclude_from_schema argument to the
@api_view have now been removed.
APIView you should instead set a
schema = None attribute on the view class.
For function-based views the
@schema decorator can be used to exclude the view from the schema, by using
Minor fixes and improvements¶
There are a large number of minor fixes and improvements in this release. See the release notes page for a complete listing.
We’re planning to iteratively work towards OpenAPI becoming the standard schema
representation. This will mean that the
coreapi dependency will gradually become
removed, and we’ll instead generate the schema directly, rather than building
OpenAPI has clearly become the standard for specifying Web APIs, so there’s not much value any more in our schema-agnostic document model. Making this change will mean that we’ll more easily be able to take advantage of the full set of OpenAPI functionality.
This will also make a wider range of tooling available.
We’ll focus on continuing to develop the API Star library and client tool into a recommended option for generating API docs, validating API schemas, and providing a dynamic client library.
There’s also a huge amount of ongoing work on maturing the ASGI landscape, with the possibility that some of this work will eventually feed back into Django.
There will be further work on the Uvicorn web server, as well as lots of functionality planned for the Starlette web framework, which is building a foundational set of tooling for working with ASGI.