In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?

前端 未结 3 680
甜味超标
甜味超标 2020-12-06 01:14

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 01:27

    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 
    

提交回复
热议问题