Skip to main content

Django mixin to easily add buttons to any ModelAdmin

Project description

django-admin-extra-buttons

Pypi coverage Test ReadTheDocs

Buttons

This is a full rewriting of the original django-admin-extra-url. It provides decorators to easily add custom buttons to Django Admin pages.

Will be possible to create wizards, actions and/or link to external resources as well as api only views.

It provides 3 decorators:

  • button() to mark a method as extra view and show related button
  • link() This is used for "external" link, where you don't need to invoke local views.
  • view() View only decorator, this adds a new url but do not render any button.

Install

pip install django-admin-extra-buttons

After installation add it to INSTALLED_APPS

INSTALLED_APPS = (
   ...
   'admin_extra_buttons',
)

How to use it

from admin_extra_buttons.api import ExtraButtonsMixin, button, link, view

class MyModelModelAdmin(extras.ExtraButtonsMixin, admin.ModelAdmin):

    @link(label='Search On Google', change_form=True, change_list=False)
    def search_on_google(self, button):
        obj = button.context['original']
        button.href = f'http://www.google.com?q={obj.name}'

    @button() # /admin/myapp/mymodel/update_all/
    def consolidate(self, request):
        ...
        ...

    @button() # /admin/myapp/mymodel/update/10/
    def update(self, request, pk):
        # if we use `pk` in the args, the button will be in change_form
        obj = self.get_object(request, pk)
        ...

    @button(permission=lambda request, obj: request.user.is_superuser)
    def empty_table(self, request):
        if request.method == 'POST':
            self.model.objects.all().delete()
        else:
            return extras._confirm_action(self, request, self.truncate,
                                   'Continuing will erase the entire content of the table.',
                                   'Successfully executed', )

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-extra-buttons-0.1.0.tar.gz (5.7 MB view hashes)

Uploaded Source

Supported by

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