我试图提出一个基本的 CRUD,嵌套资源实现,具有以下 url:
我不知道如何将默认参数(在我的情况下为内容类型 ID)传递给视图。下面是我用过的。任何指针来完成这个,或者如果这完全是废话,有没有更好的方法来完成这个?
urlpatterns = patterns('',
url (
regex = '^/?$',
view = ParentResourceListView.as_view(),
name = 'parent_resource_list'
),
url (
regex = r'^(?P<pk>\d+)/$',
view = ParentResourceDetailView.as_view(),
name = 'parent_resource_detail'
),
url (
regex = r'^new/$',
view = ParentResourceCreateView.as_view(),
name = 'parent_resource_create'
),
url (
regex = '^(?P<pk>\d+)/delete/$',
view = ParentResourceDeleteView.as_view(),
name = 'parent_resource_delete'
),
url (
regex = '^(?P<pk>\d+)/edit/$',
view = ParentResourceUpdateView.as_view(),
name = 'parent_resource_update'
),
)
urlpatterns += patterns('',
url (
regex = r'^(?P<object_id>\d+)/child_resources$',
view = ChildResourceListView.as_view(),
name = 'parent_resource_child_resource_list',
kwargs = {
"content_type",
ContentType.objects.get_for_model(ParentResource).id
}
),
url (
regex = r'^(?P<object_id>\d+)/child_resources/(?P<pk>\d+)/$',
view = ChildResourceDetailView.as_view(),
name = 'parent_resource_child_resource_detail',
kwargs = {
"content_type",
ContentType.objects.get_for_model(ParentResource).id
}
),
url (
regex = r'^(?P<object_id>\d+)/child_resources/new/$',
view = ChildResourceCreateView.as_view(),
name = 'parent_resource_child_resource_create',
kwargs={
"content_type",
ContentType.objects.get_for_model(ParentResource).id
}
),
url (
regex = '^(?P<object_id>\d+)/child_resource/(?P<pk>\d+)/delete/$',
view = ChildResourceDeleteView.as_view(),
name = 'parent_resource_child_resource_delete',
kwargs={
"content_type",
ContentType.objects.get_for_model(ParentResource).id
}
),
url (
regex = '^(?P<object_id>\d+)/child_resource/(?P<pk>\d+)/edit/$',
view = ChildResourceUpdateView.as_view(),
name = 'parent_resource_child_resource_update',
kwargs={
"content_type",
ContentType.objects.get_for_model(ParentResource).id
}
),
)
编辑:
我使用的观点受到这篇文章的启发
class ParentResourceMixin(object):
model = ParentResource
def get_success_url(self):
return reverse('parent_resource_list')
def get_queryset(self):
return ParentResource.objects.all()
class ParentResourceListView(ParentResourceMixin, ListView):
pass
class ParentResourceDetailView(ParentResourceMixin, DetailView):
pass
class ParentResourceCreateView(ParentResourceMixin, CreateView):
pass
class ParentResourceDeleteView(ParentResourceMixin, DeleteView):
pass
class ParentResourceUpdateView(ParentResourceMixin, UpdateView):
pass
编辑(基于jpic的答案的解决方案):
子资源视图是:
class ChildResourceMixin(object):
content_type = None
model = ChildResource
def get_success_url(self):
return reverse('child_resource_list')
def get_queryset(self):
return ChildResource.objects.all()
class ChildResourceListView(ChildResourceMixin, ListView):
pass
class ChildResourceDetailView(ChildResourceMixin, DetailView):
pass
class ChildResourceCreateView(ChildResourceMixin, CreateView):
pass
class ChildResourceDeleteView(ChildResourceMixin, DeleteView):
pass
class ChildResourceUpdateView(ChildResourceMixin, UpdateView):
pass