aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/6-resource-orientated-projects.md
diff options
context:
space:
mode:
authorTom Christie2012-09-14 12:43:14 +0100
committerTom Christie2012-09-14 12:43:14 +0100
commiteb761be9d058dbfb9214f200b941496524dc0ded (patch)
treecb88c5f39baaf2109c07edccbf48d89448ec1587 /docs/tutorial/6-resource-orientated-projects.md
parent886f8b47510c830483b5adae1855593cdc3df2dc (diff)
downloaddjango-rest-framework-eb761be9d058dbfb9214f200b941496524dc0ded.tar.bz2
Flesh out resources/routers part of tutorial
Diffstat (limited to 'docs/tutorial/6-resource-orientated-projects.md')
-rw-r--r--docs/tutorial/6-resource-orientated-projects.md37
1 files changed, 32 insertions, 5 deletions
diff --git a/docs/tutorial/6-resource-orientated-projects.md b/docs/tutorial/6-resource-orientated-projects.md
index 4282c25d..0d0cfac5 100644
--- a/docs/tutorial/6-resource-orientated-projects.md
+++ b/docs/tutorial/6-resource-orientated-projects.md
@@ -1,3 +1,7 @@
+In REST framework Resources classes are just View classes that don't have any handler methods bound to them. This allows us to seperate out the behaviour of the classes from how that behaviour should be bound to a set of URLs.
+
+For instance, given our serializers
+
serializers.py
class BlogPostSerializer(URLModelSerializer):
@@ -8,21 +12,44 @@ serializers.py
class Meta:
model = Comment
+We can re-write our 4 sets of views into something more compact...
+
resources.py
class BlogPostResource(ModelResource):
serializer_class = BlogPostSerializer
model = BlogPost
- permissions = [AdminOrAnonReadonly()]
- throttles = [AnonThrottle(rate='5/min')]
+ permissions_classes = (permissions.IsAuthenticatedOrReadOnly,)
+ throttle_classes = (throttles.UserRateThrottle,)
class CommentResource(ModelResource):
serializer_class = CommentSerializer
model = Comment
- permissions = [AdminOrAnonReadonly()]
- throttles = [AnonThrottle(rate='5/min')]
+ permissions_classes = (permissions.IsAuthenticatedOrReadOnly,)
+ throttle_classes = (throttles.UserRateThrottle,)
+
+The handler methods only get bound to the actions when we define the URLConf. Here's our urls.py:
+
+ comment_root = CommentResource.as_view(actions={
+ 'get': 'list',
+ 'post': 'create'
+ })
+ comment_instance = CommentInstance.as_view(actions={
+ 'get': 'retrieve',
+ 'put': 'update',
+ 'delete': 'destroy'
+ })
+ ... # And for blog post
+
+ urlpatterns = patterns('blogpost.views',
+ url(r'^$', comment_root),
+ url(r'^(?P<pk>[0-9]+)$', comment_instance)
+ ... # And for blog post
+ )
+
+## Using Routers
-Now that we're using Resources rather than Views, we don't need to design the urlconf ourselves. The conventions for wiring up resources into views and urls are handled automatically. All we need to do is register the appropriate resources with a router, and let it do the rest. Here's our re-wired `urls.py` file.
+Right now that hasn't really saved us a lot of code. However, now that we're using Resources rather than Views, we actually don't need to design the urlconf ourselves. The conventions for wiring up resources into views and urls can be handled automatically, using `Router` classes. All we need to do is register the appropriate resources with a router, and let it do the rest. Here's our re-wired `urls.py` file.
from blog import resources
from djangorestframework.routers import DefaultRouter