在 Django 管理员中,如何隐藏模型管理员上的“保存并继续"和“保存并添加另一个"按钮? [英] In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?
问题描述
我在 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
任何线索或替代方法?
提前致谢
推荐答案
除了它的(有点笨拙的)hacking 风格之外,您还可以直接覆盖模板标签.通常更推荐覆盖模板.
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
这篇关于在 Django 管理员中,如何隐藏模型管理员上的“保存并继续"和“保存并添加另一个"按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!