aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/6-resource-orientated-projects.md
diff options
context:
space:
mode:
authorJamie Matthews2012-09-26 13:05:21 +0100
committerJamie Matthews2012-09-26 13:05:21 +0100
commit01770c53cd9045e6ea054f32b1e40b5d2ff7fe44 (patch)
tree657cb66f92d78add3b2f587754387832043168e6 /docs/tutorial/6-resource-orientated-projects.md
parentf6488cb0589d3b11fb8d831e00d1389f3fff74b6 (diff)
parent09a445b257532be69ffab69a3f62b84bfa90463d (diff)
downloaddjango-rest-framework-01770c53cd9045e6ea054f32b1e40b5d2ff7fe44.tar.bz2
Merge branch 'restframework2' of git://github.com/tomchristie/django-rest-framework into improved-view-decorators
* 'restframework2' of git://github.com/tomchristie/django-rest-framework: (56 commits) Bits of cleanup Add request.QUERY_PARAMS Add readonly 'id' field Tweak browseable API Don't display readonly fields Fix some bits of serialization Add csrf note Fix incorrect bit of tutorial Added tox.ini Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Clean up bits of templates etc Hack out bunch of unneccesary private methods on View class Clean up template tags Remove dumbass __all__ variables Remove old 'djangorestframework directories Change package name: djangorestframework -> rest_framework Dont strip final '/' Use get_script_prefix to play nicely if not installed at the root. ... Conflicts: rest_framework/decorators.py
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