Modify action select style

I want change style of actions from default to buttons
image
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