[Django] #33977: Grouping action in the ModelAdmin

6 views
Skip to first unread message

Django

unread,
Sep 2, 2022, 6:28:55 AM9/2/22
to django-...@googlegroups.com
#33977: Grouping action in the ModelAdmin
--------------------------------------------+------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
--------------------------------------------+------------------------
One can make the action dropdown visually more pleasant by introducing
groups. This does not require a lot of logic: we can easily update the
`@action` decorator to allow specifying an `action_group`, and by slightly
modifying the `get_action_choices` method, we can generate a list of
groups that contain the actions:

{{{
diff --git a/django/contrib/admin/decorators.py
b/django/contrib/admin/decorators.py
index d3ff56a59a..ad34df2e65 100644
--- a/django/contrib/admin/decorators.py
+++ b/django/contrib/admin/decorators.py
@@ -1,4 +1,4 @@
-def action(function=None, *, permissions=None, description=None):
+def action(function=None, *, permissions=None, description=None,
group=None):
"""
Conveniently add attributes to an action function::

@@ -23,6 +23,8 @@ def action(function=None, *, permissions=None,
description=None):
func.allowed_permissions = permissions
if description is not None:
func.short_description = description
+ if group is not None:
+ func.action_group = group
return func

if function is None:
diff --git a/django/contrib/admin/options.py
b/django/contrib/admin/options.py
index 8ccacd6213..ca61901fe7 100644
--- a/django/contrib/admin/options.py
+++ b/django/contrib/admin/options.py
@@ -1,4 +1,5 @@
import copy
+from collections import defaultdict
import json
import re
from functools import partial, update_wrapper
@@ -1024,11 +1025,13 @@ class ModelAdmin(BaseModelAdmin):
Return a list of choices for use in a form object. Each choice
is a
tuple (name, description).
"""
+ choices = defaultdict(list)
+ choices[None].extend(default_choices)
choices = [] + default_choices
for func, name, description in
self.get_actions(request).values():
choice = (name, description % model_format_dict(self.opts))
- choices.append(choice)
- return choices
+ choices[getattr(func, 'action_group', None)].append(choice)
+ return list(choices.items())

def get_action(self, action):
"""
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33977>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 2, 2022, 6:29:36 AM9/2/22
to django-...@googlegroups.com
#33977: Grouping action in the ModelAdmin
----------------------------------+--------------------------------------

Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Willem Van Onsem):

* Attachment "django-actions.patch" added.

Django

unread,
Sep 2, 2022, 6:37:42 AM9/2/22
to django-...@googlegroups.com
#33977: Grouping action in the ModelAdmin
----------------------------------+--------------------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #33807.

--
Ticket URL: <https://code.djangoproject.com/ticket/33977#comment:1>

Reply all
Reply to author
Forward
0 new messages