I want change style of actions from default to buttons

default html is:
<select>
<option>
<button />
</option>
<option>
<button />
</option>
<option>
<button />
</option>
</select>
and i want rendering to :
<div class="gap-2">
<button></button>
<button></button>
<button></button>
</div>
and i founed the part of code is rendering by django/form/widgets/actions.html
{% block actions-form %}
{% for field in action_form %}
{% if field.label %}
<label>{{ field.label }} {{ field }}</label>
{% else %}
{{ field }}
{% endif %}
{% endfor %}
{% endblock %}
and im trying override ModelAdmin âdef get_actions_choicesâ but not success.
How should i do ?
Please post the code you are trying to use.
Youâre still talking about the admin, right?
If thatâs the case, the template being used is django.contrib.admin.templates.admin.actions.html
. You could override that template if you wanted to change its appearance.
def get_action_choices(self, request, default_choices=models.BLANK_CHOICE_DASH):
choices = [] + default_choices
for func, name, description in self.get_actions(request).values():
button_html = format_html(
'<button type="submit" name="action" value="{}">{}</button>', name, description
)
choices.append((name, button_html))
return choices
I using GPT help me change this code, and it still not working:
Ok, Iâm a little confused with what youâre trying to do here. But for what I do see:
-
ModelAdmin.get_action_choices is not documented. That means that itâs an internal API and not intended to be overridden. (Itâs probably safe, but thereâs no guarantee that the interface is stable and wonât be changed in the future.)
-
Notice how in the original get_action_choices
how description
is formatted with model_format_dict(self.opts)
. If youâre going to render the description yourself, you would need to do the same thing.
-
get_action_choices
is called by django.contrib.admin.options.ModelAdmin.changelist_view
to select the âchoicesâ attribute of the action
field in action_form
. By default, action_form
is an instance of django.contrib.admin.helpers.ActionForm
, containing two fields - one of which is action
, which is a ChoiceField
. Youâre probably not going to want to render that form at all.
-
Even with making this change, you still need to override the appropriate template.
Iâm not seeing an âeasyâ way to do this, although Iâm sure that with enough work and fiddling around you can probably get it to do what you want.
1 Like