aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api-guide')
-rwxr-xr-xdocs/api-guide/authentication.md3
-rw-r--r--docs/api-guide/fields.md6
-rw-r--r--docs/api-guide/filtering.md14
-rwxr-xr-xdocs/api-guide/generic-views.md198
-rw-r--r--docs/api-guide/pagination.md3
-rw-r--r--docs/api-guide/parsers.md5
-rw-r--r--docs/api-guide/permissions.md12
-rw-r--r--docs/api-guide/renderers.md5
-rw-r--r--docs/api-guide/routers.md106
-rwxr-xr-xdocs/api-guide/serializers.md8
-rw-r--r--docs/api-guide/throttling.md5
-rw-r--r--docs/api-guide/viewsets.md212
12 files changed, 480 insertions, 97 deletions
diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md
index 1f08f542..c2f73901 100755
--- a/docs/api-guide/authentication.md
+++ b/docs/api-guide/authentication.md
@@ -43,7 +43,8 @@ The default authentication schemes may be set globally, using the `DEFAULT_AUTHE
)
}
-You can also set the authentication scheme on a per-view basis, using the `APIView` class based views.
+You can also set the authentication scheme on a per-view or per-viewset basis,
+using the `APIView` class based views.
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md
index 42f89f46..e117c370 100644
--- a/docs/api-guide/fields.md
+++ b/docs/api-guide/fields.md
@@ -248,6 +248,12 @@ A floating point representation.
Corresponds to `django.db.models.fields.FloatField`.
+## DecimalField
+
+A decimal representation.
+
+Corresponds to `django.db.models.fields.DecimalField`.
+
## FileField
A file representation. Performs Django's standard FileField validation.
diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md
index ed946368..53cd3e13 100644
--- a/docs/api-guide/filtering.md
+++ b/docs/api-guide/filtering.md
@@ -8,7 +8,7 @@
The default behavior of REST framework's generic list views is to return the entire queryset for a model manager. Often you will want your API to restrict the items that are returned by the queryset.
-The simplest way to filter the queryset of any view that subclasses `MultipleObjectAPIView` is to override the `.get_queryset()` method.
+The simplest way to filter the queryset of any view that subclasses `GenericAPIView` is to override the `.get_queryset()` method.
Overriding this method allows you to customize the queryset returned by the view in a number of different ways.
@@ -21,7 +21,6 @@ You can do so by filtering based on the value of `request.user`.
For example:
class PurchaseList(generics.ListAPIView)
- model = Purchase
serializer_class = PurchaseSerializer
def get_queryset(self):
@@ -44,7 +43,6 @@ For example if your URL config contained an entry like this:
You could then write a view that returned a purchase queryset filtered by the username portion of the URL:
class PurchaseList(generics.ListAPIView)
- model = Purchase
serializer_class = PurchaseSerializer
def get_queryset(self):
@@ -62,7 +60,6 @@ A final example of filtering the initial queryset would be to determine the init
We can override `.get_queryset()` to deal with URLs such as `http://example.com/api/purchases?username=denvercoder9`, and filter the queryset only if the `username` parameter is included in the URL:
class PurchaseList(generics.ListAPIView)
- model = Purchase
serializer_class = PurchaseSerializer
def get_queryset(self):
@@ -97,10 +94,11 @@ You must also set the filter backend to `DjangoFilterBackend` in your settings:
## Specifying filter fields
-If all you need is simple equality-based filtering, you can set a `filter_fields` attribute on the view, listing the set of fields you wish to filter against.
+If all you need is simple equality-based filtering, you can set a `filter_fields` attribute on the view, or viewset,
+listing the set of fields you wish to filter against.
class ProductList(generics.ListAPIView):
- model = Product
+ queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_fields = ('category', 'in_stock')
@@ -120,7 +118,7 @@ For more advanced filtering requirements you can specify a `FilterSet` class tha
fields = ['category', 'in_stock', 'min_price', 'max_price']
class ProductList(generics.ListAPIView):
- model = Product
+ queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_class = ProductFilter
@@ -183,4 +181,4 @@ For example:
[cite]: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters
[django-filter]: https://github.com/alex/django-filter
[django-filter-docs]: https://django-filter.readthedocs.org/en/latest/index.html
-[nullbooleanselect]: https://github.com/django/django/blob/master/django/forms/widgets.py \ No newline at end of file
+[nullbooleanselect]: https://github.com/django/django/blob/master/django/forms/widgets.py
diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md
index cef9a9d4..4a24b7c7 100755
--- a/docs/api-guide/generic-views.md
+++ b/docs/api-guide/generic-views.md
@@ -18,7 +18,7 @@ If the generic views don't suit the needs of your API, you can drop down to usin
Typically when using the generic views, you'll override the view, and set several class attributes.
class UserList(generics.ListCreateAPIView):
- model = User
+ queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
paginate_by = 100
@@ -26,17 +26,16 @@ Typically when using the generic views, you'll override the view, and set severa
For more complex cases you might also want to override various methods on the view class. For example.
class UserList(generics.ListCreateAPIView):
- model = User
+ queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
- def get_paginate_by(self, queryset):
+ def get_paginate_by(self):
"""
Use smaller pagination for HTML representations.
"""
- page_size_param = self.request.QUERY_PARAMS.get('page_size')
- if page_size_param:
- return int(page_size_param)
+ self.request.accepted_renderer.format == 'html':
+ return 20
return 100
For very simple cases you might want to pass through any class attributes using the `.as_view()` method. For example, your URLconf might include something the following entry.
@@ -47,6 +46,114 @@ For very simple cases you might want to pass through any class attributes using
# API Reference
+## GenericAPIView
+
+This class extends REST framework's `APIView` class, adding commonly required behavior for standard list and detail views.
+
+Each of the concrete generic views provided is built by combining `GenericAPIView`, with one or more mixin classes.
+
+### Attributes
+
+**Basic settings**:
+
+The following attributes control the basic view behavior.
+
+* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method.
+* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
+* `lookup_field` - The field that should be used to lookup individual model instances. Defaults to `'pk'`. The URL conf should include a keyword argument corresponding to this value. More complex lookup styles can be supported by overriding the `get_object()` method.
+
+**Pagination**:
+
+The following attibutes are used to control pagination when used with list views.
+
+* `paginate_by` - The size of pages to use with paginated data. If set to `None` then pagination is turned off. If unset this uses the same value as the `PAGINATE_BY` setting, which defaults to `None`.
+* `paginate_by_param` - The name of a query parameter, which can be used by the client to overide the default page size to use for pagination. If unset this uses the same value as the `PAGINATE_BY_PARAM` setting, which defaults to `None`.
+* `pagination_serializer_class` - The pagination serializer class to use when determining the style of paginated responses. Defaults to the same value as the `DEFAULT_PAGINATION_SERIALIZER_CLASS` setting.
+* `page_kwarg` - The name of a URL kwarg or URL query parameter which can be used by the client to control which page is requested. Defaults to `'page'`.
+
+**Other**:
+
+* `filter_backend` - The filter backend class that should be used for filtering the queryset. Defaults to the same value as the `FILTER_BACKEND` setting.
+* `allow_empty` - Determines if an empty list should successfully display zero results, or return a 404 response. Defaults to `True`, meaning empty lists will return sucessful `200 OK` responses, with zero results.
+* `model` - This shortcut may be used instead of setting either (or both) of the `queryset`/`serializer_class` attributes, although using the explicit style is generally preferred. If used instead of `serializer_class`, then then `DEFAULT_MODEL_SERIALIZER_CLASS` setting will determine the base serializer class.
+
+### Methods
+
+**Base methods**:
+
+#### `get_queryset(self)`
+
+Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used.
+
+May be overridden to provide dynamic behavior such as returning a queryset that is specific to the user making the request.
+
+For example:
+
+ def get_queryset(self):
+ return self.user.accounts.all()
+
+#### `get_object(self)`
+
+Returns an object instance that should be used for detail views. Defaults to using the `lookup_field` parameter to filter the base queryset.
+
+May be overridden to provide more complex behavior such as object lookups based on more than one URL kwarg.
+
+For example:
+
+ def get_object(self):
+ queryset = self.get_queryset()
+ filter = {}
+ for field in self.multiple_lookup_fields:
+ filter[field] = self.kwargs[field]
+ return get_object_or_404(queryset, **filter)
+
+#### `get_serializer_class(self)`
+
+Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute, or dynamically generating a serializer class if the `model` shortcut is being used.
+
+May be override to provide dynamic behavior such as using different serializers for read and write operations, or providing different serializers to different types of uesr.
+
+For example:
+
+ def get_serializer_class(self):
+ if self.request.user.is_staff:
+ return FullAccountSerializer
+ return BasicAccountSerializer
+
+#### `get_paginate_by(self)`
+
+Returna the page size to use with pagination. By default this uses the `paginate_by` attribute, and may be overridden by the cient if the `paginate_by_param` attribute is set.
+
+You may want to override this method to provide more complex behavior such as modifying page sizes based on the media type of the response.
+
+For example:
+
+ def get_paginate_by(self):
+ self.request.accepted_renderer.format == 'html':
+ return 20
+ return 100
+
+**Save hooks**:
+
+The following methods are provided as placeholder interfaces. They contain empty implementations and are not called directly by `GenericAPIView`, but they are overridden and used by some of the mixin classes.
+
+* `pre_save(self, obj)` - A hook that is called before saving an object.
+* `post_save(self, obj, created=False)` - A hook that is called after saving an object.
+
+**Other methods**:
+
+You won't typically need to override the following methods, although you might need to call into them if you're writing custom views using `GenericAPIView`.
+
+* `get_serializer_context(self)` - Returns a dictionary containing any extra context that should be supplied to the serializer. Defaults to including `'request'`, `'view'` and `'format'` keys.
+* `get_serializer(self, instance=None, data=None, files=None, many=False, partial=False)` - Returns a serializer instance.
+* `get_pagination_serializer(self, page)` - Returns a serializer instance to use with paginated data.
+* `paginate_queryset(self, queryset)` - Paginate a queryset if required, either returning a page object, or `None` if pagination is not configured for this view.
+* `filter_queryset(self, queryset)` - Given a queryset, filter it with whichever filter backend is in use, returning a new queryset.
+
+---
+
+# Concrete View Classes
+
The following classes are the concrete generic views. If you're using generic views this is normally the level you'll be working at unless you need heavily customized behavior.
## CreateAPIView
@@ -63,7 +170,7 @@ Used for **read-only** endpoints to represent a **collection of model instances*
Provides a `get` method handler.
-Extends: [MultipleObjectAPIView], [ListModelMixin]
+Extends: [GenericAPIView], [ListModelMixin]
## RetrieveAPIView
@@ -71,7 +178,7 @@ Used for **read-only** endpoints to represent a **single model instance**.
Provides a `get` method handler.
-Extends: [SingleObjectAPIView], [RetrieveModelMixin]
+Extends: [GenericAPIView], [RetrieveModelMixin]
## DestroyAPIView
@@ -79,7 +186,7 @@ Used for **delete-only** endpoints for a **single model instance**.
Provides a `delete` method handler.
-Extends: [SingleObjectAPIView], [DestroyModelMixin]
+Extends: [GenericAPIView], [DestroyModelMixin]
## UpdateAPIView
@@ -87,7 +194,7 @@ Used for **update-only** endpoints for a **single model instance**.
Provides `put` and `patch` method handlers.
-Extends: [SingleObjectAPIView], [UpdateModelMixin]
+Extends: [GenericAPIView], [UpdateModelMixin]
## ListCreateAPIView
@@ -95,7 +202,7 @@ Used for **read-write** endpoints to represent a **collection of model instances
Provides `get` and `post` method handlers.
-Extends: [MultipleObjectAPIView], [ListModelMixin], [CreateModelMixin]
+Extends: [GenericAPIView], [ListModelMixin], [CreateModelMixin]
## RetrieveUpdateAPIView
@@ -103,7 +210,7 @@ Used for **read or update** endpoints to represent a **single model instance**.
Provides `get`, `put` and `patch` method handlers.
-Extends: [SingleObjectAPIView], [RetrieveModelMixin], [UpdateModelMixin]
+Extends: [GenericAPIView], [RetrieveModelMixin], [UpdateModelMixin]
## RetrieveDestroyAPIView
@@ -111,7 +218,7 @@ Used for **read or delete** endpoints to represent a **single model instance**.
Provides `get` and `delete` method handlers.
-Extends: [SingleObjectAPIView], [RetrieveModelMixin], [DestroyModelMixin]
+Extends: [GenericAPIView], [RetrieveModelMixin], [DestroyModelMixin]
## RetrieveUpdateDestroyAPIView
@@ -119,62 +226,13 @@ Used for **read-write-delete** endpoints to represent a **single model instance*
Provides `get`, `put`, `patch` and `delete` method handlers.
-Extends: [SingleObjectAPIView], [RetrieveModelMixin], [UpdateModelMixin], [DestroyModelMixin]
-
----
-
-# Base views
-
-Each of the generic views provided is built by combining one of the base views below, with one or more mixin classes.
-
-## GenericAPIView
-
-Extends REST framework's `APIView` class, adding support for serialization of model instances and model querysets.
-
-**Methods**:
-
-* `get_serializer_context(self)` - Returns a dictionary containing any extra context that should be supplied to the serializer. Defaults to including `'request'`, `'view'` and `'format'` keys.
-* `get_serializer_class(self)` - Returns the class that should be used for the serializer.
-* `get_serializer(self, instance=None, data=None, files=None, many=False, partial=False)` - Returns a serializer instance.
-* `pre_save(self, obj)` - A hook that is called before saving an object.
-* `post_save(self, obj, created=False)` - A hook that is called after saving an object.
-
-
-**Attributes**:
-
-* `model` - The model that should be used for this view. Used as a fallback for determining the serializer if `serializer_class` is not set, and as a fallback for determining the queryset if `queryset` is not set. Otherwise not required.
-* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. If unset, this defaults to creating a serializer class using `self.model`, with the `DEFAULT_MODEL_SERIALIZER_CLASS` setting as the base serializer class.
-
-## MultipleObjectAPIView
-
-Provides a base view for acting on a single object, by combining REST framework's `APIView`, and Django's [MultipleObjectMixin].
-
-**See also:** ccbv.co.uk documentation for [MultipleObjectMixin][multiple-object-mixin-classy].
-
-**Attributes**:
-
-* `queryset` - The queryset that should be used for returning objects from this view. If unset, defaults to the default queryset manager for `self.model`.
-* `paginate_by` - The size of pages to use with paginated data. If set to `None` then pagination is turned off. If unset this uses the same value as the `PAGINATE_BY` setting, which defaults to `None`.
-* `paginate_by_param` - The name of a query parameter, which can be used by the client to overide the default page size to use for pagination. If unset this uses the same value as the `PAGINATE_BY_PARAM` setting, which defaults to `None`.
-
-## SingleObjectAPIView
-
-Provides a base view for acting on a single object, by combining REST framework's `APIView`, and Django's [SingleObjectMixin].
-
-**See also:** ccbv.co.uk documentation for [SingleObjectMixin][single-object-mixin-classy].
-
-**Attributes**:
-
-* `queryset` - The queryset that should be used when retrieving an object from this view. If unset, defaults to the default queryset manager for `self.model`.
-* `pk_kwarg` - The URL kwarg that should be used to look up objects by primary key. Defaults to `'pk'`. [Can only be set to non-default on Django 1.4+]
-* `slug_url_kwarg` - The URL kwarg that should be used to look up objects by a slug. Defaults to `'slug'`. [Can only be set to non-default on Django 1.4+]
-* `slug_field` - The field on the model that should be used to look up objects by a slug. If used, this should typically be set to a field with `unique=True`. Defaults to `'slug'`.
+Extends: [GenericAPIView], [RetrieveModelMixin], [UpdateModelMixin], [DestroyModelMixin]
---
# Mixins
-The mixin classes provide the actions that are used to provide the basic view behaviour. Note that the mixin classes provide action methods rather than defining the handler methods such as `.get()` and `.post()` directly. This allows for more flexible composition of behaviour.
+The mixin classes provide the actions that are used to provide the basic view behavior. Note that the mixin classes provide action methods rather than defining the handler methods such as `.get()` and `.post()` directly. This allows for more flexible composition of behavior.
## ListModelMixin
@@ -182,7 +240,7 @@ Provides a `.list(request, *args, **kwargs)` method, that implements listing a q
If the queryset is populated, this returns a `200 OK` response, with a serialized representation of the queryset as the body of the response. The response data may optionally be paginated.
-If the queryset is empty this returns a `200 OK` reponse, unless the `.allow_empty` attribute on the view is set to `False`, in which case it will return a `404 Not Found`.
+If the queryset is empty this returns a `200 OK` response, unless the `.allow_empty` attribute on the view is set to `False`, in which case it will return a `404 Not Found`.
Should be mixed in with [MultipleObjectAPIView].
@@ -208,14 +266,14 @@ Should be mixed in with [SingleObjectAPIView].
Provides a `.update(request, *args, **kwargs)` method, that implements updating and saving an existing model instance.
+Also provides a `.partial_update(request, *args, **kwargs)` method, which is similar to the `update` method, except that all fields for the update will be optional. This allows support for HTTP `PATCH` requests.
+
If an object is updated this returns a `200 OK` response, with a serialized representation of the object as the body of the response.
If an object is created, for example when making a `DELETE` request followed by a `PUT` request to the same URL, this returns a `201 Created` response, with a serialized representation of the object as the body of the response.
If the request data provided for updating the object was invalid, a `400 Bad Request` response will be returned, with the error details as the body of the response.
-A boolean `partial` keyword argument may be supplied to the `.update()` method. If `partial` is set to `True`, all fields for the update will be optional. This allows support for HTTP `PATCH` requests.
-
Should be mixed in with [SingleObjectAPIView].
## DestroyModelMixin
@@ -227,14 +285,8 @@ If an object is deleted this returns a `204 No Content` response, otherwise it w
Should be mixed in with [SingleObjectAPIView].
[cite]: https://docs.djangoproject.com/en/dev/ref/class-based-views/#base-vs-generic-views
-[MultipleObjectMixin]: https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-multiple-object/
-[SingleObjectMixin]: https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-single-object/
-[multiple-object-mixin-classy]: http://ccbv.co.uk/projects/Django/1.4/django.views.generic.list/MultipleObjectMixin/
-[single-object-mixin-classy]: http://ccbv.co.uk/projects/Django/1.4/django.views.generic.detail/SingleObjectMixin/
[GenericAPIView]: #genericapiview
-[SingleObjectAPIView]: #singleobjectapiview
-[MultipleObjectAPIView]: #multipleobjectapiview
[ListModelMixin]: #listmodelmixin
[CreateModelMixin]: #createmodelmixin
[RetrieveModelMixin]: #retrievemodelmixin
diff --git a/docs/api-guide/pagination.md b/docs/api-guide/pagination.md
index 13d4760a..912ce41b 100644
--- a/docs/api-guide/pagination.md
+++ b/docs/api-guide/pagination.md
@@ -93,7 +93,8 @@ The default pagination style may be set globally, using the `DEFAULT_PAGINATION_
You can also set the pagination style on a per-view basis, using the `ListAPIView` generic class-based view.
class PaginatedListView(ListAPIView):
- model = ExampleModel
+ queryset = ExampleModel.objects.all()
+ serializer_class = ExampleModelSerializer
paginate_by = 10
paginate_by_param = 'page_size'
diff --git a/docs/api-guide/parsers.md b/docs/api-guide/parsers.md
index a2830492..49d59d10 100644
--- a/docs/api-guide/parsers.md
+++ b/docs/api-guide/parsers.md
@@ -34,7 +34,8 @@ The default set of parsers may be set globally, using the `DEFAULT_PARSER_CLASSE
)
}
-You can also set the renderers used for an individual view, using the `APIView` class based views.
+You can also set the renderers used for an individual view, or viewset,
+using the `APIView` class based views.
class ExampleView(APIView):
"""
@@ -187,4 +188,4 @@ The following third party packages are also available.
[cite]: https://groups.google.com/d/topic/django-developers/dxI4qVzrBY4/discussion
[messagepack]: https://github.com/juanriaza/django-rest-framework-msgpack
[juanriaza]: https://github.com/juanriaza
-[djangorestframework-msgpack]: https://github.com/juanriaza/django-rest-framework-msgpack \ No newline at end of file
+[djangorestframework-msgpack]: https://github.com/juanriaza/django-rest-framework-msgpack
diff --git a/docs/api-guide/permissions.md b/docs/api-guide/permissions.md
index 0c82b2a3..db0d4b26 100644
--- a/docs/api-guide/permissions.md
+++ b/docs/api-guide/permissions.md
@@ -44,7 +44,8 @@ If not specified, this setting defaults to allowing unrestricted access:
'rest_framework.permissions.AllowAny',
)
-You can also set the authentication policy on a per-view basis, using the `APIView` class based views.
+You can also set the authentication policy on a per-view, or per-viewset basis,
+using the `APIView` class based views.
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)
@@ -100,16 +101,15 @@ This permission class ties into Django's standard `django.contrib.auth` [model p
* `POST` requests require the user to have the `add` permission on the model.
* `PUT` and `PATCH` requests require the user to have the `change` permission on the model.
* `DELETE` requests require the user to have the `delete` permission on the model.
-
-If you want to use `DjangoModelPermissions` but also allow unauthenticated users to have read permission, override the class and set the `authenticated_users_only` property to `False`. For example:
-
- class HasModelPermissionsOrReadOnly(DjangoModelPermissions):
- authenticated_users_only = False
The default behaviour can also be overridden to support custom model permissions. For example, you might want to include a `view` model permission for `GET` requests.
To use custom model permissions, override `DjangoModelPermissions` and set the `.perms_map` property. Refer to the source code for details.
+## DjangoModelPermissionsOrAnonReadOnly
+
+Similar to `DjangoModelPermissions`, but also allows unauthenticated users to have read-only access to the API.
+
## TokenHasReadWriteScope
This permission class is intended for use with either of the `OAuthAuthentication` and `OAuth2Authentication` classes, and ties into the scoping that their backends provide.
diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md
index 3c8396aa..00739422 100644
--- a/docs/api-guide/renderers.md
+++ b/docs/api-guide/renderers.md
@@ -27,7 +27,8 @@ The default set of renderers may be set globally, using the `DEFAULT_RENDERER_CL
)
}
-You can also set the renderers used for an individual view, using the `APIView` class based views.
+You can also set the renderers used for an individual view, or viewset,
+using the `APIView` class based views.
class UserCountView(APIView):
"""
@@ -127,7 +128,7 @@ An example of a view that uses `TemplateHTMLRenderer`:
"""
A view that returns a templated HTML representations of a given user.
"""
- model = Users
+ queryset = User.objects.all()
renderer_classes = (TemplateHTMLRenderer,)
def get(self, request, *args, **kwargs)
diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md
new file mode 100644
index 00000000..7495b91c
--- /dev/null
+++ b/docs/api-guide/routers.md
@@ -0,0 +1,106 @@
+<a class="github" href="routers.py"></a>
+
+# Routers
+
+> Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index... a resourceful route declares them in a single line of code.
+>
+> &mdash; [Ruby on Rails Documentation][cite]
+
+Some Web frameworks such as Rails provide functionality for automatically determining how the URLs for an application should be mapped to the logic that deals with handling incoming requests.
+
+REST framework adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs.
+
+## Usage
+
+Here's an example of a simple URL conf, that uses `DefaultRouter`.
+
+ router = routers.SimpleRouter()
+ router.register(r'users', UserViewSet, name='user')
+ router.register(r'accounts', AccountViewSet, name='account')
+ urlpatterns = router.urls
+
+There are three arguments to the `register()` method:
+
+* `prefix` - The URL prefix to use for this set of routes.
+* `viewset` - The viewset class.
+* `name` - The base to use for the URL names that are created.
+
+The example above would generate the following URL patterns:
+
+* URL pattern: `^users/$` Name: `'user-list'`
+* URL pattern: `^users/{pk}/$` Name: `'user-detail'`
+* URL pattern: `^accounts/$` Name: `'account-list'`
+* URL pattern: `^accounts/{pk}/$` Name: `'account-detail'`
+
+### Extra link and actions
+
+Any methods on the viewset decorated with `@link` or `@action` will also be routed.
+For example, a given method like this on the `UserViewSet` class:
+
+ @action(permission_classes=[IsAdminOrIsSelf])
+ def set_password(self, request, pk=None):
+ ...
+
+The following URL pattern would additionally be generated:
+
+* URL pattern: `^users/{pk}/set_password/$` Name: `'user-set-password'`
+
+# API Guide
+
+## SimpleRouter
+
+This router includes routes for the standard set of `list`, `create`, `retrieve`, `update`, `partial_update` and `destroy` actions. The viewset can also mark additional methods to be routed, using the `@link` or `@action` decorators.
+
+<table border=1>
+ <tr><th>URL Style</th><th>HTTP Method</th><th>Action</th><th>URL Name</th></tr>
+ <tr><td rowspan=2>{prefix}/</td><td>GET</td><td>list</td><td rowspan=2>{basename}-list</td></tr></tr>
+ <tr><td>POST</td><td>create</td></tr>
+ <tr><td rowspan=4>{prefix}/{lookup}/</td><td>GET</td><td>retrieve</td><td rowspan=4>{basename}-detail</td></tr></tr>
+ <tr><td>PUT</td><td>update</td></tr>
+ <tr><td>PATCH</td><td>partial_update</td></tr>
+ <tr><td>DELETE</td><td>destroy</td></tr>
+ <tr><td rowspan=2>{prefix}/{lookup}/{methodname}/</td><td>GET</td><td>@link decorated method</td><td rowspan=2>{basename}-{methodname}</td></tr>
+ <tr><td>POST</td><td>@action decorated method</td></tr>
+</table>
+
+## DefaultRouter
+
+This router is similar to `SimpleRouter` as above, but additionally includes a default API root view, that returns a response containing hyperlinks to all the list views. It also generates routes for optional `.json` style format suffixes.
+
+<table border=1>
+ <tr><th>URL Style</th><th>HTTP Method</th><th>Action</th><th>URL Name</th></tr>
+ <tr><td>[.format]</td><td>GET</td><td>automatically generated root view</td><td>api-root</td></tr></tr>
+ <tr><td rowspan=2>{prefix}/[.format]</td><td>GET</td><td>list</td><td rowspan=2>{basename}-list</td></tr></tr>
+ <tr><td>POST</td><td>create</td></tr>
+ <tr><td rowspan=4>{prefix}/{lookup}/[.format]</td><td>GET</td><td>retrieve</td><td rowspan=4>{basename}-detail</td></tr></tr>
+ <tr><td>PUT</td><td>update</td></tr>
+ <tr><td>PATCH</td><td>partial_update</td></tr>
+ <tr><td>DELETE</td><td>destroy</td></tr>
+ <tr><td rowspan=2>{prefix}/{lookup}/{methodname}/[.format]</td><td>GET</td><td>@link decorated method</td><td rowspan=2>{basename}-{methodname}</td></tr>
+ <tr><td>POST</td><td>@action decorated method</td></tr>
+</table>
+
+# Custom Routers
+
+Implementing a custom router isn't something you'd need to do very often, but it can be useful if you have specfic requirements about how the your URLs for your API are strutured. Doing so allows you to encapsulate the URL structure in a reusable way that ensures you don't have to write your URL patterns explicitly for each new view.
+
+The simplest way to implement a custom router is to subclass one of the existing router classes. The `.routes` attribute is used to template the URL patterns that will be mapped to each viewset.
+
+## Example
+
+The following example will only route to the `list` and `retrieve` actions, and unlike the routers included by REST framework, it does not use the trailing slash convention.
+
+ class ReadOnlyRouter(SimpleRouter):
+ """
+ A router for read-only APIs, which doesn't use trailing suffixes.
+ """
+ routes = [
+ (r'^{prefix}$', {'get': 'list'}, '{basename}-list'),
+ (r'^{prefix}/{lookup}$', {'get': 'retrieve'}, '{basename}-detail')
+ ]
+
+## Advanced custom routers
+
+If you want to provide totally custom behavior, you can override `BaseRouter` and override the `get_urls()` method. The method should insect the registered viewsets and return a list of URL patterns. The registered prefix, viewset and basename tuples may be inspected by accessing the `self.registry` attribute.
+
+[cite]: http://guides.rubyonrails.org/routing.html
diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md
index 2797b5f5..c48461d8 100755
--- a/docs/api-guide/serializers.md
+++ b/docs/api-guide/serializers.md
@@ -374,16 +374,20 @@ Returns the field instance that should be used to represent the pk field.
### get_nested_field
-**Signature**: `.get_nested_field(self, model_field)`
+**Signature**: `.get_nested_field(self, model_field, related_model, to_many)`
Returns the field instance that should be used to represent a related field when `depth` is specified as being non-zero.
+Note that the `model_field` argument will be `None` for reverse relationships. The `related_model` argument will be the model class for the target of the field. The `to_many` argument will be a boolean indicating if this is a to-one or to-many relationship.
+
### get_related_field
-**Signature**: `.get_related_field(self, model_field, to_many=False)`
+**Signature**: `.get_related_field(self, model_field, related_model, to_many)`
Returns the field instance that should be used to represent a related field when `depth` is not specified, or when nested representations are being used and the depth reaches zero.
+Note that the `model_field` argument will be `None` for reverse relationships. The `related_model` argument will be the model class for the target of the field. The `to_many` argument will be a boolean indicating if this is a to-one or to-many relationship.
+
### get_field
**Signature**: `.get_field(self, model_field)`
diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md
index 1abd49f4..d6de85ba 100644
--- a/docs/api-guide/throttling.md
+++ b/docs/api-guide/throttling.md
@@ -40,7 +40,8 @@ The default throttling policy may be set globally, using the `DEFAULT_THROTTLE_C
The rate descriptions used in `DEFAULT_THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period.
-You can also set the throttling policy on a per-view basis, using the `APIView` class based views.
+You can also set the throttling policy on a per-view or per-viewset basis,
+using the `APIView` class based views.
class ExampleView(APIView):
throttle_classes = (UserThrottle,)
@@ -167,4 +168,4 @@ The following is an example of a rate throttle, that will randomly throttle 1 in
[cite]: https://dev.twitter.com/docs/error-codes-responses
[permissions]: permissions.md
[cache-setting]: https://docs.djangoproject.com/en/dev/ref/settings/#caches
-[cache-docs]: https://docs.djangoproject.com/en/dev/topics/cache/#setting-up-the-cache \ No newline at end of file
+[cache-docs]: https://docs.djangoproject.com/en/dev/topics/cache/#setting-up-the-cache
diff --git a/docs/api-guide/viewsets.md b/docs/api-guide/viewsets.md
new file mode 100644
index 00000000..8af35bb8
--- /dev/null
+++ b/docs/api-guide/viewsets.md
@@ -0,0 +1,212 @@
+<a class="github" href="viewsets.py"></a>
+
+# ViewSets
+
+> After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output.
+>
+> &mdash; [Ruby on Rails Documentation][cite]
+
+
+Django REST framework allows you to combine the logic for a set of related views in a single class, called a `ViewSet`. In other frameworks you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'.
+
+A `ViewSet` class is simply **a type of class-based View, that does not provide any method handlers** such as `.get()` or `.post()`, and instead provides actions such as `.list()` and `.create()`.
+
+The method handlers for a `ViewSet` are only bound to the corresponding actions at the point of finalizing the view, using the `.as_view()` method.
+
+Typically, rather than exlicitly registering the views in a viewset in the urlconf, you'll register the viewset with a router class, that automatically determines the urlconf for you.
+
+## Example
+
+Let's define a simple viewset that can be used to listing or retrieving all the users in the system.
+
+ class UserViewSet(viewsets.ViewSet):
+ """
+ A simple ViewSet that for listing or retrieving users.
+ """
+ def list(self, request):
+ queryset = User.objects.all()
+ serializer = UserSerializer(queryset, many=True)
+ return Response(serializer.data)
+
+ def retrieve(self, request, pk=None):
+ queryset = User.objects.all()
+ user = get_object_or_404(queryset, pk=pk)
+ serializer = UserSerializer(user)
+ return Response(serializer.data)
+
+If we need to, we can bind this viewset into two seperate views, like so:
+
+ user_list = UserViewSet.as_view({'get': 'list'})
+ user_detail = UserViewSet.as_view({'get': 'retrieve'})
+
+Typically we wouldn't do this, but would instead register the viewset with a router, and allow the urlconf to be automatically generated.
+
+ router = DefaultRouter()
+ router.register(r'users', UserViewSet, name='user')
+ urlpatterns = router.urls
+
+Rather than writing your own viewsets, you'll often want to use the existing base classes that provide a default set of behavior. For example:
+
+ class UserViewSet(viewsets.ModelViewSet):
+ """
+ A viewset for viewing and editing user instances.
+ """
+ serializer_class = UserSerializer
+ queryset = User.objects.all()
+
+There are two main advantages of using a `ViewSet` class over using a `View` class.
+
+* Repeated logic can be combined into a single class. In the above example, we only need to specify the `queryset` once, and it'll be used across multiple views.
+* By using routers, we no longer need to deal with wiring up the URL conf ourselves.
+
+Both of these come with a trade-off. Using regular views and URL confs is more explicit and gives you more control. ViewSets are helpful if you want to get up and running quickly, or when you have a large API and you want to enforce a consistent URL configuration throughout.
+
+## Marking extra methods for routing
+
+The default routers included with REST framework will provide routes for a standard set of create/retrieve/update/destroy style operations, as shown below:
+
+ class UserViewSet(viewsets.VietSet):
+ """
+ Example empty viewset demonstrating the standard
+ actions that will be handled by a router class.
+
+ If you're using format suffixes, make sure to also include
+ the `format=None` keyword argument for each action.
+ """
+
+ def list(self, request):
+ pass
+
+ def create(self, request):
+ pass
+
+ def retrieve(self, request, pk=None):
+ pass
+
+ def update(self, request, pk=None):
+ pass
+
+ def partial_update(self, request, pk=None):
+ pass
+
+ def destroy(self, request, pk=None):
+ pass
+
+If you have ad-hoc methods that you need to be routed to, you can mark them as requiring routing using the `@link` or `@action` decorators. The `@link` decorator will route `GET` requests, and the `@action` decroator will route `POST` requests.
+
+For example:
+
+ from django.contrib.auth.models import User
+ from rest_framework import viewsets
+ from rest_framework.decorators import action
+ from myapp.serializers import UserSerializer
+
+ class UserViewSet(viewsets.ModelViewSet):
+ """
+ A viewset that provides the standard actions
+ """
+ queryset = User.objects.all()
+ serializer_class = UserSerializer
+
+ @action
+ def set_password(self, request, pk=None):
+ user = self.get_object()
+ serializer = PasswordSerializer(data=request.DATA)
+ if serializer.is_valid():
+ user.set_password(serializer.data['password'])
+ user.save()
+ return Response({'status': 'password set'})
+ else:
+ return Response(serializer.errors,
+ status=status.HTTP_400_BAD_REQUEST)
+
+The `@action` and `@link` decorators can additionally take extra arguments that will be set for the routed view only. For example...
+
+ @action(permission_classes=[IsAdminOrIsSelf])
+ def set_password(self, request, pk=None):
+ ...
+
+---
+
+# API Reference
+
+## ViewSet
+
+The `ViewSet` class inherits from `APIView`. You can use any of the standard attributes such as `permission_classes`, `authentication_classes` in order to control the API policy on the viewset.
+
+The `ViewSet` class does not provide any implementations of actions. In order to use a `ViewSet` class you'll override the class and define the action implementations explicitly.
+
+## ModelViewSet
+
+The `ModelViewSet` class inherits from `GenericAPIView` and includes implementations for various actions, by mixing in the behavior of the
+
+The actions provided by the `ModelViewSet` class are `.list()`, `.retrieve()`, `.create()`, `.update()`, and `.destroy()`.
+
+#### Example
+
+Because `ModelViewSet` extends `GenericAPIView`, you'll normally need to provide at least the `queryset` and `serializer_class` attributes. For example:
+
+ class AccountViewSet(viewsets.ModelViewSet):
+ """
+ A simple ViewSet for viewing and editing accounts.
+ """
+ queryset = Account.objects.all()
+ serializer_class = AccountSerializer
+ permission_classes = [IsAccountAdminOrReadOnly]
+
+Note that you can use any of the standard attributes or method overrides provided by `GenericAPIView`. For example, to use a `ViewSet` that dynamically determines the queryset it should operate on, you might do something like this:
+
+ class AccountViewSet(viewsets.ModelViewSet):
+ """
+ A simple ViewSet for viewing and editing the accounts
+ associated with the user.
+ """
+ serializer_class = AccountSerializer
+ permission_classes = [IsAccountAdminOrReadOnly]
+
+ def get_queryset(self):
+ return request.user.accounts.all()
+
+Also note that although this class provides the complete set of create/list/retrieve/update/destroy actions by default, you can restrict the available operations by using the standard permission classes.
+
+## ReadOnlyModelViewSet
+
+The `ReadOnlyModelViewSet` class also inherits from `GenericAPIView`. As with `ModelViewSet` it also includes implementations for various actions, but unlike `ModelViewSet` only provides the 'read-only' actions, `.list()` and `.retrieve()`.
+
+#### Example
+
+As with `ModelViewSet`, you'll normally need to provide at least the `queryset` and `serializer_class` attributes. For example:
+
+ class AccountViewSet(viewsets.ReadOnlyModelViewSet):
+ """
+ A simple ViewSet for viewing accounts.
+ """
+ queryset = Account.objects.all()
+ serializer_class = AccountSerializer
+
+Again, as with `ModelViewSet`, you can use any of the standard attributes and method overrides available to `GenericAPIView`.
+
+# Custom ViewSet base classes
+
+Any standard `View` class can be turned into a `ViewSet` class by mixing in `ViewSetMixin`. You can use this to define your own base classes.
+
+## Example
+
+For example, we can create a base viewset class that provides `retrieve`, `update` and `list` operations:
+
+ class RetrieveUpdateListViewSet(mixins.RetrieveModelMixin,
+ mixins.UpdateModelMixin,
+ mixins.ListModelMixin,
+ viewsets.ViewSetMixin,
+ generics.GenericAPIView):
+ """
+ A viewset that provides `retrieve`, `update`, and `list` actions.
+
+ To use it, override the class and set the `.queryset` and
+ `.serializer_class` attributes.
+ """
+ pass
+
+By creating your own base `ViewSet` classes, you can provide common behavior that can be reused in multiple views across your API.
+
+[cite]: http://guides.rubyonrails.org/routing.html