aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/6-resource-orientated-projects.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorial/6-resource-orientated-projects.md')
-rw-r--r--docs/tutorial/6-resource-orientated-projects.md57
1 files changed, 42 insertions, 15 deletions
diff --git a/docs/tutorial/6-resource-orientated-projects.md b/docs/tutorial/6-resource-orientated-projects.md
index 4282c25d..3c3e7fed 100644
--- a/docs/tutorial/6-resource-orientated-projects.md
+++ b/docs/tutorial/6-resource-orientated-projects.md
@@ -1,31 +1,56 @@
-serializers.py
+# Tutorial 6 - Resources
- class BlogPostSerializer(URLModelSerializer):
- class Meta:
- model = BlogPost
+Resource classes are just View classes that don't have any handler methods bound to them. The actions on a resource are defined,
- class CommentSerializer(URLModelSerializer):
- class Meta:
- model = Comment
+This allows us to:
+
+* Encapsulate common behaviour accross a class of views, in a single Resource class.
+* Seperate out the actions of a Resource from the specfics of how those actions should be bound to a particular set of URLs.
+
+## Refactoring to use Resources, not Views
+
+For instance, 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,)
+
+## Binding Resources to URLs explicitly
+The handler methods only get bound to the actions when we define the URLConf. Here's our urls.py:
-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.
+ 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
+
+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
+ from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(resources.BlogPostResource)
@@ -44,6 +69,8 @@ We've reached the end of our tutorial. If you want to get more involved in the
* Contribute on GitHub by reviewing issues, and submitting issues or pull requests.
* Join the REST framework group, and help build the community.
-* Follow me [on Twitter](https://twitter.com/_tomchristie) and say hi.
+* Follow me [on Twitter][twitter] and say hi.
+
+**Now go build some awesome things.**
-Now go build something great. \ No newline at end of file
+[twitter]: https://twitter.com/_tomchristie