django admin - 覆盖changelist_view

时间:2016-06-27 23:25:35

标签: python django django-models django-admin

我有两个管理类用于两个模型,即app1和app2。 App1将成为app2的父模型。

class App2Admin(...):
    def changelist_view(self, request, extra_context=None):
        return super(App2Admin, self).changelist_view(request, extra_context)

class App1Admin(...):
    def get_urls(self):
        info = self.model._meta.app_label, self.model._meta.model_name
        urls = patterns('',
            url(r'^(.+)/app2/$', self.admin_site.admin_view(self.code_view), name='%s_%s_codes' % info),
            url(r'^(.+)/app2/(.+)/$', self.admin_site.admin_view(self.code_view), name='app1_app2_change'),
        )
        return urls + super(App1Admin, self).get_urls()

    def code_view(self, request, app1_id):
        "View all app2 for a given app1"
        request.app1_obj = get_object_or_404(App1_model, pk=app1_id)
        return App2Admin(Code, site).changelist_view(request, {'app1': request.app1_obj})

我的问题在于网址。当我访问app1时,我正在获取正确的网址 localhost:/ admin / app / app1 / app1_id / ,当我访问app2页面时,我的网址搞砸了像这样 localhost:/ admin / app / app1 / app2 / app2_id /

我想要做的是将URL中的app1和app2的id保留为 localhost:/ admin / app / app1 / app1_id / app2 / app2_id /

我仍然对如何正确定制changelist_view()处理我的网址感到困惑。

0 个答案:

没有答案