django-rest-framework/docs/index.md

388 lines
16 KiB
Markdown
Raw Normal View History

<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>
2014-01-03 17:06:41 +04:00
<p class="badges" height=20px>
<iframe src="http://ghbtns.com/github-btn.html?user=tomchristie&amp;repo=django-rest-framework&amp;type=watch&amp;count=true" class="github-star-button" allowtransparency="true" frameborder="0" scrolling="0" width="110px" height="20px"></iframe>
<a href="http://travis-ci.org/tomchristie/django-rest-framework?branch=master">
<img src="https://secure.travis-ci.org/tomchristie/django-rest-framework.svg?branch=master" class="status-badge">
</a>
<a href="https://pypi.python.org/pypi/djangorestframework">
<img src="https://img.shields.io/pypi/v/djangorestframework.svg" class="status-badge">
</a>
</p>
2014-01-03 17:06:41 +04:00
---
2015-10-22 16:05:52 +03:00
**Note**: This is the documentation for the **version 3** of REST framework. Documentation for [version 2](http://tomchristie.github.io/rest-framework-2-docs/) is also available.
2014-12-01 11:54:12 +03:00
---
2014-01-03 17:06:41 +04:00
<p>
2014-01-14 18:47:34 +04:00
<h1 style="position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
border: 0;">Django REST Framework</h1>
<img alt="Django REST Framework" title="Logo by Jake 'Sid' Smith" src="img/logo.png" width="600px" style="display: block; margin: 0 auto 0 auto">
2014-01-03 17:06:41 +04:00
</p>
2012-09-01 23:26:27 +04:00
2015-10-22 16:05:52 +03:00
Django REST framework is a powerful and flexible toolkit for building Web APIs.
2012-09-01 23:26:27 +04:00
2013-04-30 12:38:04 +04:00
Some reasons you might want to use REST framework:
2012-09-01 23:26:27 +04:00
2014-11-29 22:23:55 +03:00
* The [Web browsable API][sandbox] is a huge usability win for your developers.
* [Authentication policies][authentication] including packages for [OAuth1a][oauth1-section] and [OAuth2][oauth2-section].
2013-06-27 16:41:42 +04:00
* [Serialization][serializers] that supports both [ORM][modelserializer-section] and [non-ORM][serializer-section] data sources.
* Customizable all the way down - just use [regular function-based views][functionview-section] if you don't need the [more][generic-views] [powerful][viewsets] [features][routers].
* [Extensive documentation][index], and [great community support][group].
* Used and trusted by internationally recognised companies including [Mozilla][mozilla], [Red Hat][redhat], [Heroku][heroku], and [Eventbrite][eventbrite].
2012-10-30 16:23:17 +04:00
2014-01-03 17:06:41 +04:00
---
2012-10-30 16:23:17 +04:00
## Funding
2016-05-28 12:55:38 +03:00
REST framework is a *collaboratively funded project*. If you use
REST framework commercially we strongly encourage you to invest in its
continued development by **[signing up for a paid plan][funding]**.
The initial aim is to provide a single full-time position on REST framework.
Right now we're over 58% of the way towards achieving that.
2016-07-05 17:58:36 +03:00
*Every single sign-up makes a significant impact.*
<ul class="premium-promo promo">
<li><a href="http://jobs.rover.com/" style="background-image: url(https://fund-rest-framework.s3.amazonaws.com/rover_130x130.png)">Rover.com</a></li>
<li><a href="https://getsentry.com/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>
</ul>
<div style="clear: both; padding-bottom: 20px;"></div>
*Many thanks to all our [awesome sponsors][sponsors], and in particular to our premium backers, [Rover](http://jobs.rover.com/), [Sentry](https://getsentry.com/welcome/), and [Stream](https://getstream.io/?utm_source=drf&utm_medium=banner&utm_campaign=drf).*
2012-10-30 16:23:17 +04:00
2016-05-28 12:55:38 +03:00
---
2014-01-03 17:06:41 +04:00
2012-09-01 23:26:27 +04:00
## Requirements
REST framework requires the following:
2015-10-22 16:05:52 +03:00
* Python (2.7, 3.2, 3.3, 3.4, 3.5)
2015-09-28 13:44:07 +03:00
* Django (1.7+, 1.8, 1.9)
2012-09-01 23:26:27 +04:00
The following packages are optional:
* [coreapi][coreapi] (1.21.0+) - Schema generation support.
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
* [django-filter][django-filter] (0.9.2+) - Filtering support.
2015-10-22 16:05:52 +03:00
* [django-crispy-forms][django-crispy-forms] - Improved HTML display for filtering.
* [django-guardian][django-guardian] (1.1.1+) - Object level permissions support.
2013-03-07 13:01:53 +04:00
2012-09-01 23:26:27 +04:00
## Installation
Install using `pip`, including any optional packages you want...
2012-09-01 23:26:27 +04:00
pip install djangorestframework
2013-05-05 21:12:35 +04:00
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
2012-09-01 23:26:27 +04:00
...or clone the project from github.
git clone git@github.com:tomchristie/django-rest-framework.git
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
2012-09-01 23:26:27 +04:00
INSTALLED_APPS = (
...
'rest_framework',
2012-09-01 23:26:27 +04:00
)
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
2012-09-01 23:26:27 +04:00
urlpatterns = [
2012-09-01 23:26:27 +04:00
...
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace. You may leave out the namespace in Django 1.9+, and REST framework will set it for you.
2013-04-25 01:40:24 +04:00
## Example
Let's take a look at a quick example of using REST framework to build a simple model-backed API.
2014-08-20 19:24:52 +04:00
We'll create a read-write API for accessing information on the users of our project.
2013-04-25 01:40:24 +04:00
2013-04-30 17:34:42 +04:00
Any global settings for a REST framework API are kept in a single configuration dictionary named `REST_FRAMEWORK`. Start off by adding the following to your `settings.py` module:
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS`.
We're ready to create our API now.
2013-04-30 11:24:33 +04:00
Here's our project's root `urls.py` module:
2013-04-29 16:20:15 +04:00
2014-08-20 19:24:52 +04:00
from django.conf.urls import url, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
2013-04-25 01:40:24 +04:00
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
2014-08-20 19:24:52 +04:00
queryset = User.objects.all()
serializer_class = UserSerializer
# Routers provide an easy way of automatically determining the URL conf.
2013-04-29 16:20:15 +04:00
router = routers.DefaultRouter()
2013-05-16 11:47:22 +04:00
router.register(r'users', UserViewSet)
2013-04-30 11:24:33 +04:00
2013-04-29 16:20:15 +04:00
# Wire up our API using automatic URL routing.
2014-11-29 22:23:55 +03:00
# Additionally, we include login URLs for the browsable API.
2014-08-20 19:24:52 +04:00
urlpatterns = [
2013-04-25 01:40:24 +04:00
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
2014-08-20 19:24:52 +04:00
]
You can now open the API in your browser at [http://127.0.0.1:8000/](http://127.0.0.1:8000/), and view your new 'users' API. If you use the login control in the top right corner you'll also be able to add, create and delete users from the system.
2013-04-25 01:40:24 +04:00
2012-09-01 23:26:27 +04:00
## Quickstart
2012-08-29 23:57:37 +04:00
2014-08-20 19:24:52 +04:00
Can't wait to get started? The [quickstart guide][quickstart] is the fastest way to get up and running, and building APIs with REST framework.
2012-09-01 23:26:27 +04:00
## Tutorial
The tutorial will walk you through the building blocks that make up REST framework. It'll take a little while to get through, but it'll give you a comprehensive understanding of how everything fits together, and is highly recommended reading.
2012-08-29 23:57:37 +04:00
* [1 - Serialization][tut-1]
* [2 - Requests & Responses][tut-2]
* [3 - Class based views][tut-3]
2012-10-28 23:37:27 +04:00
* [4 - Authentication & permissions][tut-4]
2012-08-29 23:57:37 +04:00
* [5 - Relationships & hyperlinked APIs][tut-5]
2013-04-09 14:54:51 +04:00
* [6 - Viewsets & routers][tut-6]
2016-07-07 11:55:26 +03:00
* [7 - Schemas & client libraries][tut-7]
2012-08-29 23:57:37 +04:00
2014-01-03 17:06:41 +04:00
There is a live example API of the finished tutorial API for testing purposes, [available here][sandbox].
2012-09-01 23:26:27 +04:00
## API Guide
The API guide is your complete reference manual to all the functionality provided by REST framework.
2012-08-29 23:57:37 +04:00
* [Requests][request]
* [Responses][response]
* [Views][views]
2012-09-12 13:12:13 +04:00
* [Generic views][generic-views]
2013-04-09 14:54:51 +04:00
* [Viewsets][viewsets]
* [Routers][routers]
2012-08-29 23:57:37 +04:00
* [Parsers][parsers]
* [Renderers][renderers]
* [Serializers][serializers]
2012-10-05 20:10:07 +04:00
* [Serializer fields][fields]
2012-12-31 12:53:40 +04:00
* [Serializer relations][relations]
2014-11-25 19:29:14 +03:00
* [Validators][validators]
2012-08-29 23:57:37 +04:00
* [Authentication][authentication]
* [Permissions][permissions]
2012-09-05 13:01:43 +04:00
* [Throttling][throttling]
2012-11-08 01:28:10 +04:00
* [Filtering][filtering]
2012-10-01 19:17:01 +04:00
* [Pagination][pagination]
* [Versioning][versioning]
2012-09-12 13:12:13 +04:00
* [Content negotiation][contentnegotiation]
2014-12-18 18:42:27 +03:00
* [Metadata][metadata]
* [Schemas][schemas]
2012-09-12 13:12:13 +04:00
* [Format suffixes][formatsuffixes]
* [Returning URLs][reverse]
2012-09-01 23:26:27 +04:00
* [Exceptions][exceptions]
2012-08-29 23:57:37 +04:00
* [Status codes][status]
2013-08-16 17:12:24 +04:00
* [Testing][testing]
2012-09-05 13:01:43 +04:00
* [Settings][settings]
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
## Topics
General guides to using REST framework.
2012-08-29 23:57:37 +04:00
* [Documenting your API][documenting-your-api]
* [API Clients][api-clients]
2015-10-12 23:14:58 +03:00
* [Internationalization][internationalization]
2013-01-27 21:23:56 +04:00
* [AJAX, CSRF & CORS][ajax-csrf-cors]
2015-10-12 23:14:58 +03:00
* [HTML & Forms][html-and-forms]
2012-10-13 18:07:43 +04:00
* [Browser enhancements][browser-enhancements]
2012-10-13 18:34:38 +04:00
* [The Browsable API][browsableapi]
2012-10-08 15:17:43 +04:00
* [REST, Hypermedia & HATEOAS][rest-hypermedia-hateoas]
* [Third Party Resources][third-party-resources]
2013-12-04 18:59:09 +04:00
* [Contributing to REST framework][contributing]
2014-12-18 16:35:42 +03:00
* [Project management][project-management]
2014-12-01 11:54:12 +03:00
* [3.0 Announcement][3.0-announcement]
2015-02-06 03:04:20 +03:00
* [3.1 Announcement][3.1-announcement]
2015-08-06 16:18:23 +03:00
* [3.2 Announcement][3.2-announcement]
2015-10-22 16:05:52 +03:00
* [3.3 Announcement][3.3-announcement]
2014-07-17 20:17:41 +04:00
* [Kickstarter Announcement][kickstarter-announcement]
2016-05-26 13:48:15 +03:00
* [Mozilla Grant][mozilla-grant]
* [Funding][funding]
2012-10-17 16:51:15 +04:00
* [Release Notes][release-notes]
2012-08-29 23:57:37 +04:00
2012-09-02 00:35:30 +04:00
## Development
See the [Contribution guidelines][contributing] for information on how to clone
the repository, run the test suite and contribute changes back to REST
Framework.
2013-03-06 16:19:49 +04:00
2012-10-09 15:01:17 +04:00
## Support
2013-08-29 23:04:00 +04:00
For support please see the [REST framework discussion group][group], try the `#restframework` channel on `irc.freenode.net`, search [the IRC archives][botbot], or raise a question on [Stack Overflow][stack-overflow], making sure to include the ['django-rest-framework'][django-rest-framework-tag] tag.
2012-10-09 15:01:17 +04:00
[Paid support is available][paid-support] from [DabApps][dabapps], and can include work on REST framework core, or support with building your REST framework API. Please [contact DabApps][contact-dabapps] if you'd like to discuss commercial support options.
2012-10-09 15:01:17 +04:00
2013-01-24 17:01:42 +04:00
For updates on REST framework development, you may also want to follow [the author][twitter] on Twitter.
<a style="padding-top: 10px" href="https://twitter.com/_tomchristie" class="twitter-follow-button" data-show-count="false">Follow @_tomchristie</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
2013-05-22 19:45:33 +04:00
## Security
If you believe youve found something in Django REST framework which has security implications, please **do not raise the issue in a public forum**.
Send a description of the issue via email to [rest-framework-security@googlegroups.com][security-mail]. The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.
2012-09-01 23:26:27 +04:00
## License
2016-01-11 17:13:07 +03:00
Copyright (c) 2011-2016, Tom Christie
2012-09-01 23:26:27 +04:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
2012-09-01 23:26:27 +04:00
modification, are permitted provided that the following conditions are met:
2012-08-29 23:57:37 +04:00
Redistributions of source code must retain the above copyright notice, this
2012-09-01 23:26:27 +04:00
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
2012-09-01 23:26:27 +04:00
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2012-09-01 23:26:27 +04:00
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2014-01-03 17:06:41 +04:00
[mozilla]: http://www.mozilla.org/en-US/about/
[redhat]: https://www.redhat.com/
[heroku]: https://www.heroku.com/
2014-01-03 17:06:41 +04:00
[eventbrite]: https://www.eventbrite.co.uk/about/
[coreapi]: http://pypi.python.org/pypi/coreapi/
2012-09-01 23:26:27 +04:00
[markdown]: http://pypi.python.org/pypi/Markdown/
2013-01-12 17:31:40 +04:00
[django-filter]: http://pypi.python.org/pypi/django-filter
[django-crispy-forms]: https://github.com/maraujop/django-crispy-forms
2013-09-12 23:27:23 +04:00
[django-guardian]: https://github.com/lukaszb/django-guardian
2012-10-30 16:23:17 +04:00
[0.4]: https://github.com/tomchristie/django-rest-framework/tree/0.4.X
[image]: img/quickstart.png
2013-06-27 16:41:42 +04:00
[index]: .
[oauth1-section]: api-guide/authentication/#django-rest-framework-oauth
[oauth2-section]: api-guide/authentication/#django-oauth-toolkit
[serializer-section]: api-guide/serializers#serializers
[modelserializer-section]: api-guide/serializers#modelserializer
[functionview-section]: api-guide/views#function-based-views
2012-10-30 16:23:17 +04:00
[sandbox]: http://restframework.herokuapp.com/
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors
2012-08-29 23:57:37 +04:00
2012-10-09 15:01:17 +04:00
[quickstart]: tutorial/quickstart.md
2012-08-29 23:57:37 +04:00
[tut-1]: tutorial/1-serialization.md
[tut-2]: tutorial/2-requests-and-responses.md
[tut-3]: tutorial/3-class-based-views.md
2012-10-28 23:37:27 +04:00
[tut-4]: tutorial/4-authentication-and-permissions.md
2012-08-29 23:57:37 +04:00
[tut-5]: tutorial/5-relationships-and-hyperlinked-apis.md
2013-03-31 14:36:58 +04:00
[tut-6]: tutorial/6-viewsets-and-routers.md
[tut-7]: tutorial/7-schemas-and-client-libraries.md
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
[request]: api-guide/requests.md
[response]: api-guide/responses.md
[views]: api-guide/views.md
2012-09-12 13:12:13 +04:00
[generic-views]: api-guide/generic-views.md
2013-04-09 14:54:51 +04:00
[viewsets]: api-guide/viewsets.md
[routers]: api-guide/routers.md
2012-09-01 23:26:27 +04:00
[parsers]: api-guide/parsers.md
[renderers]: api-guide/renderers.md
[serializers]: api-guide/serializers.md
2012-10-05 20:10:07 +04:00
[fields]: api-guide/fields.md
2012-12-31 12:53:40 +04:00
[relations]: api-guide/relations.md
2014-11-25 19:29:14 +03:00
[validators]: api-guide/validators.md
2012-09-01 23:26:27 +04:00
[authentication]: api-guide/authentication.md
[permissions]: api-guide/permissions.md
2012-09-05 13:01:43 +04:00
[throttling]: api-guide/throttling.md
2012-11-08 01:28:10 +04:00
[filtering]: api-guide/filtering.md
2012-10-01 19:17:01 +04:00
[pagination]: api-guide/pagination.md
[versioning]: api-guide/versioning.md
2012-09-12 13:12:13 +04:00
[contentnegotiation]: api-guide/content-negotiation.md
2014-12-18 18:42:27 +03:00
[metadata]: api-guide/metadata.md
[schemas]: 'api-guide/schemas.md'
2012-09-12 13:12:13 +04:00
[formatsuffixes]: api-guide/format-suffixes.md
[reverse]: api-guide/reverse.md
2012-09-01 23:26:27 +04:00
[exceptions]: api-guide/exceptions.md
2012-09-05 16:03:55 +04:00
[status]: api-guide/status-codes.md
2013-08-16 17:12:24 +04:00
[testing]: api-guide/testing.md
2012-09-05 13:01:43 +04:00
[settings]: api-guide/settings.md
2012-09-01 23:26:27 +04:00
[documenting-your-api]: topics/documenting-your-api.md
[api-clients]: topics/api-clients.md
2015-10-12 23:14:58 +03:00
[internationalization]: topics/internationalization.md
2013-01-27 21:23:56 +04:00
[ajax-csrf-cors]: topics/ajax-csrf-cors.md
2015-10-12 23:14:58 +03:00
[html-and-forms]: topics/html-and-forms.md
2012-10-13 18:07:43 +04:00
[browser-enhancements]: topics/browser-enhancements.md
2012-09-10 00:46:05 +04:00
[browsableapi]: topics/browsable-api.md
2012-10-08 15:17:43 +04:00
[rest-hypermedia-hateoas]: topics/rest-hypermedia-hateoas.md
2012-09-05 16:03:55 +04:00
[contributing]: topics/contributing.md
2014-12-18 16:35:42 +03:00
[project-management]: topics/project-management.md
[third-party-resources]: topics/third-party-resources.md
2014-12-01 11:54:12 +03:00
[3.0-announcement]: topics/3.0-announcement.md
2015-02-06 03:04:20 +03:00
[3.1-announcement]: topics/3.1-announcement.md
2015-08-06 16:18:23 +03:00
[3.2-announcement]: topics/3.2-announcement.md
2015-10-22 16:05:52 +03:00
[3.3-announcement]: topics/3.3-announcement.md
[kickstarter-announcement]: topics/kickstarter-announcement.md
2016-05-26 13:48:15 +03:00
[mozilla-grant]: topics/mozilla-grant.md
2015-08-13 16:17:27 +03:00
[funding]: topics/funding.md
2012-10-17 16:51:15 +04:00
[release-notes]: topics/release-notes.md
2012-10-09 15:01:17 +04:00
2013-03-06 16:19:49 +04:00
[tox]: http://testrun.org/tox/latest/
2012-10-09 15:01:17 +04:00
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
2013-08-29 23:04:00 +04:00
[botbot]: https://botbot.me/freenode/restframework/
[stack-overflow]: http://stackoverflow.com/
[django-rest-framework-tag]: http://stackoverflow.com/questions/tagged/django-rest-framework
[django-tag]: http://stackoverflow.com/questions/tagged/django
2013-05-22 19:45:33 +04:00
[security-mail]: mailto:rest-framework-security@googlegroups.com
[paid-support]: http://dabapps.com/services/build/api-development/
[dabapps]: http://dabapps.com
[contact-dabapps]: http://dabapps.com/contact/
2013-01-24 17:01:42 +04:00
[twitter]: https://twitter.com/_tomchristie