More buttons in admin
Add more buttons with custom behavior to your admin's submit line.
Using
- Add
admin_buttons
to INSTALLED_APPS
:
INSTALLED_APPS = [
...,
"admin_buttons",
]
- Mix our class in your
ModelAdmin
, and define a admin_buttons_config
attribute:
from admin_buttons.admin import AdminButtonsMixin
from django.contrib import admin
from django.db import models
from django.shortcuts import redirect
class SpecialModelAdmin[M: models.Model](AdminButtonsMixin, admin.ModelAdmin[M]):
"""
ModelAdmin for models that do something special. Their special thing
will be done upon the click of a button
"""
admin_buttons_config = [
{
"name": "_dosomethingspecial",
"method": "do_something_special",
"label": _("Do something very special"),
"condition": lambda request, context: (
request.user.has_perm("someperm")
and not re.search(r"add/?$", request.path)
),
"extra_html": mark_safe_lazy(
'<input type="number" step=1 min=1 max=99 value=1 '
'name="n_times" aria-label="'
f'{_("Number of times do to something special")}">'
),
use_separator: True,
},
]
def do_something_special(self, request: HttpRequest, obj: M | None):
obj.something_special()
return redirect(request.META["HTTP_REFERER"])
Development
All contributions are welcome! To setup development:
pip install -r dev.requirements.txt
pre-commit install