18

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

4 Answers 4

17

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 
8
  • 2
    I think this is actually the best approach. First, I tried to do what you explain in # put this in some app such as customize/templatetags/admin_modify.py ... but it didn't work. Then, I put this piece of code at the beginning of the admin.py file and it works perfect. Oct 29, 2012 at 10:21
  • 2
    @ManuelAlvarez fine. Perhaps your customize/templatetags/ lacked __init__.py and was thus not a package and got ignored. I'm not quite sure whether your admin.py solution works in all circumstances, we'll see :)
    – okm
    Oct 29, 2012 at 11:09
  • 1
    what's the purpose of that import * ? I tried to delete it, and import all those functions one by one, but my custom submit_row method is not called anymore. Any ideeas why ? Oct 10, 2013 at 7:51
  • 1
    @NiCU It's a quick typing to import all requisites. Importing one by one is more appropriate. Perhaps your tag does not override the original one, because your custom app places after the original app in INSTALLED_APPS?
    – okm
    Oct 11, 2013 at 5:25
  • yeah, i had some issues there now it's fixed (umm... i guess i should delete my comments since are not really useful) Oct 11, 2013 at 7:53
13

This isn't possible with an 'out of the box' option as far as I can tell, but this is how I'd go about doing what you want to do.

The bit of code we care about is this templatetag - this seems to override show_save_and_add_another and show_save_and_continue regardless of what you have set it to. It also creates a whole new context and copies only certain values across (not clear what the justification for this is), so you'll have to modify it to get what you need.

So:

  1. Create a templatetag that replicates the functionality of the default tag, either by reusing the existing one (see okm's example) or by duplicating it entirely. The only change here is that it should either keep your show_save_and_add_another from the original context without overwriting it, or pass through your own really_hide_save_and_add_another_damnit context variable.
  2. Replace change_form.html to include and use your own templatetag, replacing submit_row with it.
  3. Update change_form.html if you've gone for the option of using an extra context variable, wrapping the buttons with another conditional statement.

Then, regardless of what option you went for, update your ModelAdmin with something like (based on this from the Django docs):

class MyModelAdmin(admin.ModelAdmin):
    # ...
    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context['show_save_and_add_another'] = False
        # or
        extra_context['really_hide_save_and_add_another_damnit'] = True
        return super(MyModelAdmin, self).change_view(request, object_id,
            form_url, extra_context=extra_context)

Updated: Original response didn't take in to account the submit_row not passing along any the whole original context.

2
  • It works well and I can test my 'really_hide_save_and_add_another_damnit' new var on change_form.html but it seems that I can't do it in submit_line.html since its value is '' in all cases. I've tried to test, check, print,... the value without success. Do I need to load any aditional library at the beginning of the template? Oct 28, 2012 at 7:50
  • Sorry I didn't realise the submit_row templatetag doesn't copy the whole context across - only certain items. You'll need to override this templatetag, either entirely, or using okm's option.
    – Tom
    Oct 28, 2012 at 9:41
5

A very different approach can be to add the following in /static/admin/css/base.css

input[name="_addanother"],input[name="_continue"],input[name="_saveasnew"]{
    display: none;
}
0

To remove "Save and continue editing" button and "Save and add another" button, set "False" to "extra_context['show_save_and_continue']" in "changeform_view()" and return "False" in "has_add_permission()" respectively as shown below:

# "admin.py"

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        extra_context = extra_context or {}

        extra_context['show_save_and_continue'] = False # Here

        return super().changeform_view(request, object_id, form_url, extra_context)

    def has_add_permission(self, request, obj=None): # Here
        return False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.