加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用Django 1.8的show_change_link,“保存”按钮返回

发布时间:2020-12-20 13:52:51 所属栏目:Python 来源:网络整理
导读:作为Django 1.8中的一个新功能,我们有InlineModelAdmin.show_change_link,用文档的话来说, Specifies whether or not inline objects that can be changed in the admin have a link to the change form. 这很棒,我一直在寻找这个功能很长一段时间. 只是一个
作为Django 1.8中的一个新功能,我们有InlineModelAdmin.show_change_link,用文档的话来说,

Specifies whether or not inline objects that can be changed in the admin have a link to the change form.

这很棒,我一直在寻找这个功能很长一段时间.

只是一个问题.

假设我在管理员更改表单中为某个具有内联的模型的实例(例如,考虑轮询模型,使用选择内联,来自Django tutorial).我使用新的“更改”链接转到其中一个选项的完整更改表单.我进行了一些编辑,然后单击“保存”.

我希望收回我来自的地方 – 也就是说,转到Poll实例的更改表单.相反,我被带到所有Choice实例的列表中.

我怎么能让Django记住,如果我来自一个内联列表,我应该回到那里“保存”? (但如果我直接从所有选择列表中编辑选择,我应该回到那里.)

解决方法

你可以通过在完整的Choice管理员上覆盖response_change方法让Django回到Poll实例:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

class ChoiceAdmin(admin.ModelAdmin):
    '''
    To be called from the Poll choice inline. It will send control back 
    to the Poll change form,not the Choice change list.
    '''
    fields = [...]

    def response_change(self,request,choice):
        if not '_continue' in request.POST:
            return HttpResponseRedirect(reverse("admin:appname_poll_change",args=(choice.poll.id,)))
        else:
            return super(ChoiceAdmin,self).response_change(request,choice)

要解决问题的第二部分:我认为您必须在模型上注册第二个未经修改的管理员.您可以使用代理模型执行此操作.见Multiple ModelAdmins/views for same model in Django admin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读