Skip to content

Commit d4efaac

Browse files
authored
feat: optionally hide default change list/form actions (#1658)
1 parent 57e5ab8 commit d4efaac

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

docs/actions/introduction.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,26 @@ All these actions are based on custom URLs generated for each of them. Handler f
126126
For actions without intermediate steps, you can write all the logic inside handler directly. Request and object ID are both passed to these action handler functions, so you are free to fetch the instance from database and perform any operations with it. In the end, it is recommended to return redirect back to either detail or listing based on where the action was triggered from.
127127

128128
For actions with intermediate steps, it is recommended to use handler function only to redirect to custom URL with custom view. This view can be extended from base Unfold view, to have unified experience.
129+
130+
131+
## Hide built-in actions
132+
133+
By default, Django and third-party packages add their own actions, which are displayed alongside any custom actions you define. In some situations, you may want to completely hide these built-in actions, this can help save horizontal space, use different icons or texts, or move actions into a dropdown menu for a cleaner interface. To hide these default actions, set `actions_list_hide_default` or `actions_detail_hide_default` to `True`.
134+
135+
```python
136+
from django.utils.translation import gettext_lazy as _
137+
from django.shortcuts import redirect
138+
139+
from unfold.admin import ModelAdmin
140+
from unfold.decorators import action
141+
142+
143+
class MyAdmin(ModelAdmin):
144+
actions_list = ["my_action", "existing_action_wrapper"]
145+
actions_list_hide_default = True
146+
147+
@action(description=_("History"), icon="history")
148+
def existing_action_wrapper(self, *args, **kwargs):
149+
# Redirect to the page URL which is created by third-party package
150+
return redirect("https://example.com")
151+
```

src/unfold/mixins/action_model_admin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class ActionModelAdminMixin:
1818
"""
1919

2020
actions_list = () # Displayed in changelist at the top
21+
actions_list_hide_default = False
2122
actions_row = () # Displayed in changelist for each row in the table
2223
actions_detail = () # Displayed in changeform at the top
24+
actions_detail_hide_default = False
2325
actions_submit_line = () # Displayed in changeform in the submit line (form buttons)
2426

2527
def changelist_view(
@@ -51,6 +53,7 @@ def changelist_view(
5153

5254
extra_context.update(
5355
{
56+
"actions_list_hide_default": self.actions_list_hide_default,
5457
"actions_list": actions_list,
5558
"actions_row": actions_row,
5659
}
@@ -85,6 +88,7 @@ def changeform_view(
8588

8689
extra_context.update(
8790
{
91+
"actions_detail_hide_default": self.actions_detail_hide_default,
8892
"actions_submit_line": actions_submit_line,
8993
"actions_detail": actions_detail,
9094
}

src/unfold/templates/unfold/helpers/tab_actions.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{% if actions_list or actions_detail or actions_items or nav_global %}
22
<ul class="flex flex-col font-medium mb-4 ml-auto mt-2 md:flex-row md:mb-0 md:mt-0 max-md:w-full">
3-
{% if actions_items %}
4-
{{ actions_items }}
5-
{% endif %}
3+
{% if not actions_list_hide_default and not actions_detail_hide_default %}
4+
{% if actions_items %}
5+
{{ actions_items }}
6+
{% endif %}
67

7-
{% if nav_global %}
8-
{{ nav_global }}
8+
{% if nav_global %}
9+
{{ nav_global }}
10+
{% endif %}
911
{% endif %}
1012

1113
{% for action in actions_list %}

src/unfold/templatetags/unfold.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def tab_list(context: RequestContext, page: str, opts: Options | None = None) ->
6969
data = {
7070
"nav_global": context.get("nav_global"),
7171
"actions_detail": context.get("actions_detail"),
72+
"actions_detail_hide_default": context.get("actions_detail_hide_default"),
7273
"actions_list": context.get("actions_list"),
74+
"actions_list_hide_default": context.get("actions_list_hide_default"),
7375
"actions_items": context.get("actions_items"),
7476
"is_popup": context.get("is_popup"),
7577
"tabs_list": _get_tabs_list(context, page, opts),

0 commit comments

Comments
 (0)