Skip to main content

Add more buttons with custom behavior to your django admin's submit line.

Project description

More buttons in admin

Add more buttons with custom behavior to your admin's submit line.

Using

  1. Add admin_buttons to INSTALLED_APPS:
INSTALLED_APPS = [
    ...,
    "admin_buttons",
]
  1. 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 = [
        {
            # the button html name
            "name": "_dosomethingspecial",

            # the method to be called when clicking the button
            "method": "do_something_special",

            # The button label
            "label": _("Do something very special"),

            # Optionally, define a condition. If it does not pass, the
            # button will not be displayed
            "condition": lambda request, context: (
                request.user.has_perm("someperm")
                and not re.search(r"add/?$", request.path)
            ),

            # Optionally, include extra html after the button
            "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")}">'
            ),

            # Optionally, use a separator before this button:
            use_separator: True,
        },
    ]

    # The chosen method works as a view. Therefore, it should return a response
    # To simply return to the change view, redirect to HTTP_REFERER, as below:
    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:

  1. pip install -r dev.requirements.txt
  2. pre-commit install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_more_buttons-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_more_buttons-0.0.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_more_buttons-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django_admin_more_buttons-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f3a9692ebe586c0362bf0e9dd5759403638ceb754f4927af841047ed3a148a9e
MD5 3b7d7380246701668194a3fa5587b803
BLAKE2b-256 1bcc3822defe6b895c69e56a2dea68d5db73ac38d60bf1ba6e9da00442d15f63

See more details on using hashes here.

File details

Details for the file django_admin_more_buttons-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_more_buttons-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 96d9f35e3b8cd6c4cbcb13f65cb023cba016e7356e40152c9fb0f58c67896c38
MD5 502d98d58357430c600137d2c68015f4
BLAKE2b-256 55900248bf0d39162b28d3e535ac4d0bad1c62a07066543da98addd01d07994c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page