In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin?
django, django-admin, django-templates, python, python-3.x
Solution
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
Problem
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. 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. I have tried to add it as extra_context: ``` extra_context = { 'show_save_and_add_another': False, 'show_save_and_continue': False } ``` and pass it through ModelAdmin.add_view or ModelAdmin.change_view but it doesn't work. This is only for one model, so I don't want to remove from submit_line.html Any clue or alternative way? Thanks in advance