且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-01 23:11:58

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

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