I was able to remove the "save and add another" and "save and continue editing" buttons by implementing the following code:
# At the start of my admin.py file I have:
from django.contrib.admin.templatetags.admin_modify import *
from django.contrib.admin.templatetags.admin_modify import submit_row as original_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
class MyModelAdmin(GuardedModelAdmin):
# Then inside MyModelAdmin I have this:
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
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).change_view(request, object_id,
form_url, extra_context=extra_context)
This works great when I'm using my change_view, but when I'm adding a new instance of the model, the buttons reappear. I tried the following:
def add_view(self, request, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['show_save_and_add_another'] = False
extra_context['show_save_and_continue'] = False
return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
But it gives me a bizarre MissingAtrribute error -- here's the traceback:
Traceback:
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/home/username/Development/MyProject/webapp/MyModel/admin.py" in add_view
153. return super(MyModelAdmin, self).add_view(self, request, form_url='', extra_context=extra_context)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
95. result = middleware.process_view(request, view_func, args, kwargs)
File "/home/username/.virtualenvs/MyProject/lib/python3.3/site-packages/django/middleware/csrf.py" in process_view
111. request.COOKIES[settings.CSRF_COOKIE_NAME])
Exception Type: AttributeError at /admin/MyModel/ModelInstance/add/
Exception Value: 'MyModelAdmin' object has no attribute 'COOKIES'
I'm using django-guardian and wondering if this is somehow causing my problem? Does anyone know how to get rid of these annoying buttons from the submit_line part of the template when adding a new model instance?