4

我正在尝试将两个 URL 添加到模型管理员。

class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category_view(self, request):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])
                },
            context_instance=RequestContext(request)
            )

    def get_urls(self):
        urls = super(JobTitleAdmin, self).get_urls()
        custom_urls = patterns('',
            url(r'^add_to_job_category/$', self.admin_site.admin_view(self.add_to_job_category_view), name='admin_jobtitle_add_to_cat',),
            url(r'^move_to_job_category/$', self.admin_site.admin_view(self.move_to_job_category_view), name='admin_jobtitle_move_to_cat',)
    )

        return custom_urls + urls

为简单起见,我省略了视图代码。

这两种模式没有定义,因为如果我尝试访问它们,我会得到 404,并且它们没有在 django debud 404 页面中列出。然而 get_urls 方法被正确调用(实际上是两次,不知道为什么)。

有什么问题?

4

2 回答 2

2

我已经成功地做到了,但是我复制了ModelAdmin.get_urls wrap装饰器。也许尝试:

from functools import update_wrapper
class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category_view(self, request):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])
                },
            context_instance=RequestContext(request)
            )

    def get_urls(self):
        urls = super(JobTitleAdmin, self).get_urls()

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        custom_urls = patterns('',
            url(r'^add_to_job_category/$',
                 wrap(self.add_to_job_category_view),
                 name='admin_jobtitle_add_to_cat',),
            url(r'^move_to_job_category/$',
                 wrap(self.move_to_job_category_view),
                 name='admin_jobtitle_move_to_cat',)
        )

        return custom_urls + urls
于 2013-03-07T03:37:05.357 回答
0

从您的代码中,您似乎正在尝试向您的类添加一些操作中间视图ModelAdmin

这是通过将函数名称放入actions属性中来实现的。如果您只是添加自定义操作,则无需添加自定义 url。您的示例可以这样修改:

class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category(self, request, queryset):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])  # <-- this would probably be changed to use `queryset` function argument
                },
            context_instance=RequestContext(request)
            )
    add_to_job_category.short_description = "Add to job category"

该类ModelAdmin将知道如何调用您的自定义函数并将操作添加到操作下拉列表中。中间响应视图add_to_job_category需要知道如何从操作中获取输入以确定选择了哪些项目。

如果您真的想添加自定义 url,那么您的get_urls()功能似乎是正确的。请注意,返回的 urlget_urls是相对于admin/<app>/<model>.

如果您的应用程序被命名myapp并且您的模型JobTitle,那么点击您的add_to_job_category视图的 url 将是:

http://some.site/admin/myapp/jobtitle/add_to_job_category/
于 2013-03-06T00:43:33.793 回答