Opened 2 years ago

Closed 2 years ago

#33977 closed New feature (duplicate)

Grouping action in the ModelAdmin

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

Description

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):
         """

Attachments (1)

django-actions.patch (1.7 KB ) - added by Willem Van Onsem 2 years ago.

Download all attachments as: .zip

Change History (2)

by Willem Van Onsem, 2 years ago

Attachment: django-actions.patch added

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #33807.

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