mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Tweaks
This commit is contained in:
parent
71e55cc4f6
commit
4a7139e41d
|
@ -44,23 +44,25 @@ To see what's going on under the hood let's first explicitly create a set of vie
|
|||
|
||||
In the `urls.py` file we first need to bind our resources to concrete views.
|
||||
|
||||
snippet_list = SnippetResource.as_view(actions={
|
||||
from snippets import resources
|
||||
|
||||
snippet_list = resources.SnippetResource.as_view({
|
||||
'get': 'list',
|
||||
'post': 'create'
|
||||
})
|
||||
snippet_detail = SnippetResource.as_view(actions={
|
||||
snippet_detail = resources.SnippetResource.as_view({
|
||||
'get': 'retrieve',
|
||||
'put': 'update',
|
||||
'delete': 'destroy'
|
||||
})
|
||||
snippet_highlight = SnippetResource.as_view(actions={
|
||||
snippet_highlight = resources.SnippetResource.as_view({
|
||||
'get': 'highlight'
|
||||
})
|
||||
user_list = UserResource.as_view(actions={
|
||||
user_list = resources.UserResource.as_view({
|
||||
'get': 'list',
|
||||
'post': 'create'
|
||||
})
|
||||
user_detail = UserResource.as_view(actions={
|
||||
user_detail = resources.UserResource.as_view({
|
||||
'get': 'retrieve',
|
||||
'put': 'update',
|
||||
'delete': 'destroy'
|
||||
|
@ -93,12 +95,12 @@ Replace the remainder of the `urls.py` file with the following:
|
|||
|
||||
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 snippets import resources
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
router = DefaultRouter(include_root=True, include_format_suffixes=True)
|
||||
router.register(resources.SnippetResource)
|
||||
router.register(resources.UserResource)
|
||||
router = DefaultRouter()
|
||||
router.register('snippets', resources.SnippetResource)
|
||||
router.register('users', resources.UserResource)
|
||||
urlpatterns = router.urlpatterns
|
||||
|
||||
## Trade-offs between views vs resources.
|
||||
|
|
33
rest_framework/routers.py
Normal file
33
rest_framework/routers.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
# Not properly implemented yet, just the basic idea
|
||||
|
||||
|
||||
class BaseRouter(object):
|
||||
def __init__(self):
|
||||
self.resources = []
|
||||
|
||||
def register(self, name, resource):
|
||||
self.resources.append((name, resource))
|
||||
|
||||
@property
|
||||
def urlpatterns(self):
|
||||
ret = []
|
||||
|
||||
for name, resource in self.resources:
|
||||
list_actions = {
|
||||
'get': getattr(resource, 'list', None),
|
||||
'post': getattr(resource, 'create', None)
|
||||
}
|
||||
detail_actions = {
|
||||
'get': getattr(resource, 'retrieve', None),
|
||||
'put': getattr(resource, 'update', None),
|
||||
'delete': getattr(resource, 'destroy', None)
|
||||
}
|
||||
list_regex = r'^%s/$' % name
|
||||
detail_regex = r'^%s/(?P<pk>[0-9]+)/$' % name
|
||||
list_name = '%s-list'
|
||||
detail_name = '%s-detail'
|
||||
|
||||
ret += url(list_regex, resource.as_view(list_actions), list_name)
|
||||
ret += url(detail_regex, resource.as_view(detail_actions), detail_name)
|
||||
|
||||
return ret
|
Loading…
Reference in New Issue
Block a user