在Django管理员中,如何在模型管理员上隐藏保存并继续并保存并添加其他按钮?

问题描述:

我在Django管理员中有一个与用户工作流程非常相似的模型的工作流程。首先,我有一个带有基本字段的表单,然后是另一个表格,其余的数据。

I have a workflow for a model in the Django admin that is very similar to the users' workflow. First, I have a form with basic fields and then, a second form with the rest of the data.

与auth.user相同的工作流程

It's the same workflow as auth.user

我需要删除保存并继续和保存并添加其他按钮,以防止用户破坏工作流。

I need to remove "save and continue" and "save and add another" buttons to prevent the user breakoing the workflow.

我试图将其添加为extra_context

I have tried to add it as extra_context

extra_context = {
  'show_save_and_add_another': False,
  'show_save_and_continue': False
}

,并通过ModelAdmin.add_view或ModelAdmin.change_view传递它不起作用。

and pass it through ModelAdmin.add_view or ModelAdmin.change_view but it doesn't work.

这只适用于一个型号,所以我不想从submit_line.html

This is only for one model, so I don't want to remove from submit_line.html

任何线索或替代方式?

提前感谢

除了(有点尴尬)黑客风格之外,您可以直接覆盖模板标签。
更推荐通常覆盖模板。

Beside its (a bit awkward) hacking style, you could aslo override the template tag directly. Normally overriding template is more recommended.

# put this in some app such as customize/templatetags/admin_modify.py and place the app
# before the 'django.contrib.admin' in the INSTALLED_APPS in settings

from django.contrib.admin.templatetags.admin_modify import *
from django.contrib.admin.templatetags.admin_modify import submit_row as original_submit_row
# or 
# original_submit_row = submit_row

@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def submit_row(context):
    ctx = original_submit_row(context)
    ctx.update({
        'show_save_and_add_another': context.get('show_save_and_add_another', ctx['show_save_and_add_another']),
        'show_save_and_continue': context.get('show_save_and_continue', ctx['show_save_and_continue'])
        })                                                                  
    return ctx