Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A pattern for implementing RESTfull nested resources in Django
from django.conf.urls import patterns, url
from django.contrib.contenttypes.models import ContentType
from appname1.views import PostListView, PostDetailView, \
PostCreateView, PostDeleteView, PostUpdateView
from appname2.views import CommentListView,\
CommentDetailView, CommentCreateView, CommentDeleteView, \
CommentUpdateView
from models import Post
urlpatterns = patterns('',
url (
regex = '^/?$',
view = PostListView.as_view(),
name = 'post_list'
),
url (
regex = r'^(?P<pk>\d+)/$',
view = PostDetailView.as_view(),
name = 'post_detail'
),
url (
regex = r'^new/$',
view = PostCreateView.as_view(),
name = 'post_create'
),
url (
regex = '^(?P<pk>\d+)/delete/$',
view = PostDeleteView.as_view(),
name = 'post_delete'
),
url (
regex = '^(?P<pk>\d+)/edit/$',
view = PostUpdateView.as_view(),
name = 'post_update'
),
)
post_content_type = ContentType.objects.get_for_model(Post)
urlpatterns += patterns('',
url (
regex = r'^(?P<object_id>\d+)/comments$',
view = CommentListView.as_view(
content_type=post_content_type
),
name = 'post_comment_list',
),
url (
regex = r'^(?P<object_id>\d+)/comments/(?P<pk>\d+)/$',
view = CommentDetailView.as_view(
content_type=post_content_type
),
name = 'post_comment_detail',
),
url (
regex = r'^(?P<object_id>\d+)/comments/new/$',
view = CommentCreateView.as_view(
content_type=post_content_type
),
name = 'post_comment_create',
),
url (
regex = '^(?P<object_id>\d+)/comment/(?P<pk>\d+)/delete/$',
view = CommentDeleteView.as_view(
content_type=post_content_type
),
name = 'post_comment_delete',
),
url (
regex = '^(?P<object_id>\d+)/comment/(?P<pk>\d+)/edit/$',
view = CommentUpdateView.as_view(
content_type=post_content_type
),
name = 'post_comment_update',
),
)
from django.views.generic import ListView, DetailView, CreateView,\
DeleteView, UpdateView
from appname1.models import Post
class PostCBVMixin(object):
model = Post
def get_success_url(self):
return reverse('post_list')
def get_queryset(self):
return Post.objects.all()
class PostListView(PostCBVMixin, ListView):
pass
class PostDetailView(PostCBVMixin, DetailView):
pass
class PostCreateView(PostCBVMixin, CreateView):
pass
class PostDeleteView(PostCBVMixin, DeleteView):
pass
class PostUpdateView(PostCBVMixin, UpdateView):
pass
from django.views.generic import ListView, DetailView, CreateView,\
DeleteView, UpdateView
from appname2.models import Comment
class CommentCBVMixin(object):
model = Comment
content_type = None
def get_success_url(self):
return reverse('comment_list')
def get_queryset(self):
return Comment.objects.filter(
content_type__pk=self.content_type,
object_id=object_id
)
def get_template_names(self):
def prepend_app_model_name(template_name):
return "/".join([
self.content_type.app_label,
self.content_type.name,
template_name
])
names = super(CommentCBVMixin, self).get_template_names()
names = map(prepend_app_model_name, names)
return names
def form_valid(self, form):
form.instance.content_type_id = self.content_type.id
form.instance.object_id = self.kwargs.get('object_id')
return super(CommentCBVMixin, self).form_valid(form)
class CommentListView(CommentCBVMixin, ListView):
pass
class CommentDetailView(CommentCBVMixin, DetailView):
pass
class CommentCreateView(CommentCBVMixin, CreateView):
pass
class CommentDeleteView(CommentCBVMixin, DeleteView):
pass
class CommentUpdateView(CommentCBVMixin, UpdateView):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.