1

我试图提出一个基本的 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
4

1 回答 1

1

CBV 文档

如果您只更改基于类的视图上的一些简单属性,您可以简单地将它们传递给 as_view 方法调用本身

ParentResourceListView.as_view(
    content_type=ContentType.objects.get_for_model(ParentResource).id)

为此,您应该定义一个类属性,即。

class ChildResourceMixin(object):
    content_type = None

这将启用从 as_view() 传递的参数。

我喜欢你的代码,它看起来简单而灵活。

于 2012-08-29T09:34:56.547 回答