#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:1 by , 5 years ago
comment:2 by , 5 years ago
| Has patch: | set |
|---|---|
| Needs tests: | set |
Proposed patch:
https://github.com/django/django/pull/13520