aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/generic-views.md
diff options
context:
space:
mode:
authorTom Christie2012-10-02 21:26:15 +0100
committerTom Christie2012-10-02 21:26:15 +0100
commitb89125ef53a2f9e246afd5eda5c8f404a714da76 (patch)
treeb0bccbb0f5b24abd41385394cf4a6fa22c2bbb39 /docs/api-guide/generic-views.md
parenteaecffe211eff12310d37ac6104af7c64ad37d34 (diff)
downloaddjango-rest-framework-b89125ef53a2f9e246afd5eda5c8f404a714da76.tar.bz2
Update view docs slightly
Diffstat (limited to 'docs/api-guide/generic-views.md')
-rw-r--r--docs/api-guide/generic-views.md43
1 files changed, 35 insertions, 8 deletions
diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md
index 88f245c7..b2284ae5 100644
--- a/docs/api-guide/generic-views.md
+++ b/docs/api-guide/generic-views.md
@@ -9,9 +9,38 @@
One of the key benefits of class based views is the way they allow you to compose bits of reusable behaviour. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.
-## Example
+The generic views provided by REST framework allow you to quickly build API views that map closely to your database models.
-...
+If the generic views don't suit the needs of your API, you can drop down to using the regular `APIView` class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views.
+
+## Examples
+
+Typically when using the generic views, you'll override the view, and set several class attributes.
+
+ class UserList(generics.ListCreateAPIView):
+ serializer = UserSerializer
+ model = User
+ permissions = (IsAdminUser,)
+ paginate_by = 100
+
+For more complex cases you might also want to override various methods on the view class. For example.
+
+ class UserList(generics.ListCreateAPIView):
+ serializer = UserSerializer
+ model = User
+ permissions = (IsAdminUser,)
+
+ def get_paginate_by(self):
+ """
+ Use smaller pagination for HTML representations.
+ """
+ if self.request.accepted_media_type == 'text/html':
+ return 10
+ 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.
+
+ url(r'^/users/', ListCreateAPIView.as_view(model=User) name='user-list')
---
@@ -19,7 +48,7 @@ One of the key benefits of class based views is the way they allow you to compos
## ListAPIView
-Used for read-write endpoints to represent a collection of model instances.
+Used for read-only endpoints to represent a collection of model instances.
Provides a `get` method handler.
@@ -45,6 +74,8 @@ Provides `get`, `put` and `delete` method handlers.
# Base views
+Each of the generic views provided is built by combining one of the base views below, with one or more mixin classes.
+
## BaseAPIView
Extends REST framework's `APIView` class, adding support for serialization of model instances and model querysets.
@@ -65,7 +96,7 @@ Provides a base view for acting on a single object, by combining REST framework'
# Mixins
-The mixin classes provide the actions that are used
+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.
## ListModelMixin
@@ -87,10 +118,6 @@ Provides a `.update(request, *args, **kwargs)` method, that implements updating
Provides a `.destroy(request, *args, **kwargs)` method, that implements deletion of an existing model instance.
-## MetadataMixin
-
-Provides a `.metadata(request, *args, **kwargs)` method, that returns a response containing metadata about the view.
-
[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/