Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32099 closed Cleanup/optimization (duplicate)

Admin action decorators

Reported by: Michal Dabski Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: admin, actions, decorator
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Currently django admin actions can be customized by adding arbitrary properties to the action method or function (allowed_permissions , short_description). This method is not very discoverable, and rather un-puthonic.

Proposed solution:
Django should provide a built-in decorator that wrap action callable and adds any properties passed to the decorator. Because decorator can define available properties in it's signature, this method will be less error-prone and will make it easier to discover properties available to admin actions.
As an added bonus, a decorator could maybe even add the action to the actions list without the user having to do it explicitly.

Example:

@admin.register(models.Document)
class DocumentAdmin(ModelAdmin):
    actions = 'publish',

    def publish(self, request, qs):
        qs.update(publish=True)
    publish.short_description = _('publish document')
    publish.allowed_permissions =[ 'change']

could become:

@admin.register(models.Document)
class DocumentAdmin(ModelAdmin):
    actions = 'publish',

    @action(short_description=_('publish document'), allowed_permissions=['change'])
    def publish(self, request, qs):
        qs.update(publish=True)

This functionality is implemented by a third-party library, but it has not been updated in 9 years:
https://github.com/kmike/django-admin-decorators

Change History (4)

comment:2 by Michal Dabski, 4 years ago

Has patch: set
Needs tests: set

comment:3 by Mariusz Felisiak, 4 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #16117.

comment:4 by GitHub <noreply@…>, 4 years ago

In 9204485:

Fixed #16117 -- Added decorators for admin action and display functions.

Refs #25134, #32099.

Note: See TracTickets for help on using tickets.
Back to Top