django-rest-framework/docs/tutorial/6-resource-orientated-projects.md

77 lines
3.0 KiB
Markdown
Raw Normal View History

2012-09-19 16:02:10 +04:00
# Tutorial 6 - Resources
2012-09-19 16:02:10 +04:00
Resource classes are just View classes that don't have any handler methods bound to them. The actions on a resource are defined,
2012-09-19 16:02:10 +04:00
This allows us to:
2012-08-29 23:57:37 +04:00
* Encapsulate common behaviour across a class of views, in a single Resource class.
2012-10-21 18:34:07 +04:00
* Separate out the actions of a Resource from the specifics of how those actions should be bound to a particular set of URLs.
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
## Refactoring to use Resources, not Views
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
For instance, we can re-write our 4 sets of views into something more compact...
2012-08-29 23:57:37 +04:00
resources.py
class BlogPostResource(ModelResource):
serializer_class = BlogPostSerializer
model = BlogPost
permissions_classes = (permissions.IsAuthenticatedOrReadOnly,)
throttle_classes = (throttles.UserRateThrottle,)
2012-08-29 23:57:37 +04:00
class CommentResource(ModelResource):
serializer_class = CommentSerializer
model = Comment
permissions_classes = (permissions.IsAuthenticatedOrReadOnly,)
throttle_classes = (throttles.UserRateThrottle,)
2012-09-19 16:02:10 +04:00
## Binding Resources to URLs explicitly
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
2012-08-29 23:57:37 +04:00
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.
2012-08-29 23:57:37 +04:00
from blog import resources
from rest_framework.routers import DefaultRouter
2012-08-29 23:57:37 +04:00
router = DefaultRouter()
router.register(resources.BlogPostResource)
router.register(resources.CommentResource)
urlpatterns = router.urlpatterns
## Trade-offs between views vs resources.
Writing resource-oriented code can be a good thing. It helps ensure that URL conventions will be consistent across your APIs, and minimises the amount of code you need to write.
2012-08-29 23:57:37 +04:00
The trade-off is that the behaviour is less explict. It can be more difficult to determine what code path is being followed, or where to override some behaviour.
## Onwards and upwards.
We've reached the end of our tutorial. If you want to get more involved in the REST framework project, here's a few places you can start:
* Contribute on GitHub by reviewing issues, and submitting issues or pull requests.
* Join the REST framework group, and help build the community.
2012-09-19 16:02:10 +04:00
* Follow me [on Twitter][twitter] and say hi.
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
**Now go build some awesome things.**
[twitter]: https://twitter.com/_tomchristie