django-rest-framework/docs/index.md

337 lines
14 KiB
Markdown
Raw Normal View History

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="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>
<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>
2013-06-04 23:23:16 +04:00
<img src="https://secure.travis-ci.org/tomchristie/django-rest-framework.png?branch=master" class="travis-build-image">
</p>
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
2014-01-03 17:06:41 +04:00
<!--
# Django REST framework
-->
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
2013-06-27 16:41:42 +04:00
* The [Web browseable API][sandbox] is a huge usability win for your developers.
* [Authentication policies][authentication] including [OAuth1a][oauth1-section] and [OAuth2][oauth2-section] out of the box.
* [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:
2013-02-15 13:05:55 +04:00
* Python (2.6.5+, 2.7, 3.2, 3.3)
* Django (1.4.2+, 1.5, 1.6, 1.7)
2012-09-01 23:26:27 +04:00
The following packages are optional:
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
* [PyYAML][yaml] (3.10+) - YAML content-type support.
* [defusedxml][defusedxml] (0.3+) - XML content-type support.
* [django-filter][django-filter] (0.5.4+) - Filtering support.
2013-03-07 13:01:53 +04:00
* [django-oauth-plus][django-oauth-plus] (2.0+) and [oauth2][oauth2] (1.5.211+) - OAuth 1.0a support.
2013-03-07 21:50:48 +04:00
* [django-oauth2-provider][django-oauth2-provider] (0.2.3+) - OAuth 2.0 support.
* [django-guardian][django-guardian] (1.1.1+) - Object level permissions support.
2013-03-07 13:01:53 +04:00
**Note**: The `oauth2` Python package is badly misnamed, and actually provides OAuth 1.0a support. Also note that packages required for both OAuth 1.0a, and OAuth 2.0 are not yet Python 3 compatible.
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.
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.
2013-04-25 01:40:24 +04:00
# Additionally, we include login URLs for the browseable 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]
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]
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]
2012-09-12 13:12:13 +04:00
* [Content negotiation][contentnegotiation]
* [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]
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]
* [Third Party Resources][third-party-resources]
2013-12-04 18:59:09 +04:00
* [Contributing to REST framework][contributing]
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-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
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
2014-01-02 13:28:34 +04:00
Copyright (c) 2011-2014, 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.
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/
[yaml]: http://pypi.python.org/pypi/PyYAML
[defusedxml]: https://pypi.python.org/pypi/defusedxml
2013-01-12 17:31:40 +04:00
[django-filter]: http://pypi.python.org/pypi/django-filter
2013-03-07 13:01:53 +04:00
[oauth2]: https://github.com/simplegeo/python-oauth2
[django-oauth-plus]: https://bitbucket.org/david/django-oauth-plus/wiki/Home
2013-03-07 21:50:48 +04:00
[django-oauth2-provider]: https://github.com/caffeinehit/django-oauth2-provider
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#oauthauthentication
[oauth2-section]: api-guide/authentication#oauth2authentication
[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
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
2012-09-12 13:12:13 +04:00
[contentnegotiation]: api-guide/content-negotiation.md
[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
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
[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
[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/
[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