Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 892
Manuel Alvarez Avatar asked Oct 27 '12 15:10

Manuel Alvarez


People also ask

How do you remove save and add another Django admin?

The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.

How do I restrict admin in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.


2 Answers

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 
like image 58
okm Avatar answered Oct 23 '22 11:10

okm


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.

like image 35
Tom Avatar answered Oct 23 '22 11:10

Tom