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
--
Ticket URL: <https://code.djangoproject.com/ticket/32099>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Michal Dabski):
Proposed patch:
https://github.com/django/django/pull/13520
--
Ticket URL: <https://code.djangoproject.com/ticket/32099#comment:1>
Comment (by GitHub <noreply@…>):
In [changeset:"920448539631b52dcee53bd32a880abbc9de18bd" 9204485]:
{{{
#!CommitTicketReference repository=""
revision="920448539631b52dcee53bd32a880abbc9de18bd"
Fixed #16117 -- Added decorators for admin action and display functions.
Refs #25134, #32099.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32099#comment:4>