2014-01-03 17:06:41 +04:00
< p class = "badges" height = 20px >
2013-01-22 21:35:46 +04:00
< iframe src = "http://ghbtns.com/github-btn.html?user=tomchristie&repo=django-rest-framework&type=watch&count=true" class = "github-star-button" allowtransparency = "true" frameborder = "0" scrolling = "0" width = "110px" height = "20px" > < / iframe >
2014-01-08 19:22:41 +04:00
< a href = "https://twitter.com/share" class = "twitter-share-button" data-url = "django-rest-framework.org" data-text = "Checking out the totally awesome Django REST framework! http://www.django-rest-framework.org" data-count = "none" > < / a >
2013-01-22 21:35:46 +04:00
< 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 = "http://platform.twitter.com/widgets.js" ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , "script" , "twitter-wjs" ) ; < / script >
2014-12-03 15:37:56 +03:00
< img src = "https://secure.travis-ci.org/tomchristie/django-rest-framework.svg?branch=master" class = "travis-build-image" >
2013-01-22 21:35:46 +04:00
< / p >
2012-09-09 19:56:04 +04:00
2014-01-03 17:06:41 +04:00
---
2014-11-28 17:26:03 +03:00
**Note**: This is the documentation for the **version 3.0** of REST framework. Documentation for [version 2.4 ](http://tomchristie.github.io/rest-framework-2-docs/ ) is also available.
2014-11-03 17:43:00 +03:00
2014-12-01 13:48:45 +03:00
For more details see the [3.0 release notes][3.0-announcement].
2014-12-01 11:54:12 +03:00
2014-11-03 17:43:00 +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 >
2014-01-14 18:40:36 +04:00
< 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
2013-04-30 12:38:04 +04:00
Django REST framework is a powerful and flexible toolkit that makes it easy to build 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.
2015-01-05 15:26:15 +03:00
* [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].
2014-01-03 17:06:41 +04:00
* Used and trusted by large companies such as [Mozilla][mozilla] 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
![Screenshot][image]
2014-01-03 17:06:41 +04:00
**Above**: *Screenshot from the browsable API*
2012-09-01 23:26:27 +04:00
## Requirements
REST framework requires the following:
2014-09-29 19:57:40 +04:00
* Python (2.6.5+, 2.7, 3.2, 3.3, 3.4)
2015-01-08 17:51:08 +03:00
* Django (1.4.11+, 1.5.6+, 1.6.3+, 1.7)
2012-09-01 23:26:27 +04:00
The following packages are optional:
2013-05-02 08:26:40 +04:00
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
2012-11-17 00:58:49 +04:00
* [django-filter][django-filter] (0.5.4+) - Filtering support.
2013-09-06 20:01:31 +04:00
* [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
2012-09-03 15:11:57 +04:00
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.
2012-11-17 00:58:49 +04:00
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
2012-12-27 21:53:07 +04:00
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
2012-09-01 23:26:27 +04:00
INSTALLED_APPS = (
...
2014-01-14 18:40:36 +04:00
'rest_framework',
2012-09-01 23:26:27 +04:00
)
2013-05-02 08:26:40 +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
2014-09-24 01:08:38 +04:00
urlpatterns = [
2012-09-01 23:26:27 +04:00
...
2012-09-20 16:06:27 +04:00
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
2014-09-24 01:08:38 +04:00
]
2012-09-20 16:06:27 +04:00
2012-12-27 21:53:07 +04:00
Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace.
2012-09-20 16:06:27 +04:00
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
2014-01-14 18:40:36 +04:00
2013-12-13 19:30:59 +04:00
# 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
]
2014-08-21 15:25:20 +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-10-05 13:23:47 +04:00
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]
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]
2014-12-17 15:41:46 +03:00
* [Versioning][versioning]
2012-09-12 13:12:13 +04:00
* [Content negotiation][contentnegotiation]
2014-12-18 18:42:27 +03:00
* [Metadata][metadata]
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
2013-07-03 01:15:46 +04:00
* [Documenting your API][documenting-your-api]
2013-01-27 21:23:56 +04:00
* [AJAX, CSRF & CORS][ajax-csrf-cors]
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]
2014-09-13 21:53:40 +04:00
* [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]
2012-10-27 21:39:17 +04:00
* [2.0 Announcement][rest-framework-2-announcement]
2013-02-13 18:08:56 +04:00
* [2.2 Announcement][2.2-announcement]
2013-04-25 23:43:37 +04:00
* [2.3 Announcement][2.3-announcement]
2014-08-29 13:10:17 +04:00
* [2.4 Announcement][2.4-announcement]
2014-12-01 11:54:12 +03:00
* [3.0 Announcement][3.0-announcement]
2014-07-17 20:17:41 +04:00
* [Kickstarter Announcement][kickstarter-announcement]
2012-10-17 16:51:15 +04:00
* [Release Notes][release-notes]
2012-09-02 00:23:50 +04:00
* [Credits][credits]
2012-08-29 23:57:37 +04:00
2012-09-02 00:35:30 +04:00
## Development
2014-03-02 15:40:30 +04:00
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
2013-01-18 18:04:26 +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 you’ ve 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
2014-12-28 21:48:42 +03:00
Copyright (c) 2011-2015, Tom Christie
2012-09-01 23:26:27 +04:00
All rights reserved.
2014-01-14 18:40:36 +04:00
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
2014-01-14 18:40:36 +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.
2014-01-14 18:40:36 +04:00
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.
2014-01-14 18:40:36 +04:00
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.
2012-10-30 18:48:21 +04:00
[travis]: http://travis-ci.org/tomchristie/django-rest-framework?branch=master
2013-01-04 01:43:54 +04:00
[travis-build-image]: https://secure.travis-ci.org/tomchristie/django-rest-framework.png?branch=master
2014-01-03 17:06:41 +04:00
[mozilla]: http://www.mozilla.org/en-US/about/
[eventbrite]: https://www.eventbrite.co.uk/about/
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
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]: .
2015-01-05 15:26:15 +03:00
[oauth1-section]: api-guide/authentication/#django-rest-framework-oauth
[oauth2-section]: api-guide/authentication/#django-oauth-toolkit
2013-11-19 15:01:27 +04:00
[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/
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
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
2014-12-17 15:41:46 +03:00
[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
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
2013-07-03 01:15:46 +04:00
[documenting-your-api]: topics/documenting-your-api.md
2015-01-13 15:07:25 +03:00
[internationalization]: topics/documenting-your-api.md
2013-01-27 21:23:56 +04:00
[ajax-csrf-cors]: topics/ajax-csrf-cors.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
2014-09-13 21:53:40 +04:00
[third-party-resources]: topics/third-party-resources.md
2012-10-27 21:39:17 +04:00
[rest-framework-2-announcement]: topics/rest-framework-2-announcement.md
2013-02-13 18:08:56 +04:00
[2.2-announcement]: topics/2.2-announcement.md
2013-04-25 23:43:37 +04:00
[2.3-announcement]: topics/2.3-announcement.md
2014-08-29 13:10:17 +04:00
[2.4-announcement]: topics/2.4-announcement.md
2014-12-01 11:54:12 +03:00
[3.0-announcement]: topics/3.0-announcement.md
2014-09-13 21:53:40 +04:00
[kickstarter-announcement]: topics/kickstarter-announcement.md
2012-10-17 16:51:15 +04:00
[release-notes]: topics/release-notes.md
2012-09-02 00:23:50 +04:00
[credits]: topics/credits.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/
2013-01-18 18:04:26 +04:00
[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
2013-01-18 18:04:26 +04:00
[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