crccheck / django-object-actions

A Django app for easily adding object tools in the Django admin
https://pypi.org/project/django-object-actions/
Apache License 2.0
680 stars 82 forks source link

Ability to place action buttons within fields #118

Open ngirard opened 3 years ago

ngirard commented 3 years ago

Hi, let me start by expressing my gratitude for your efforts into this nice project !

When actions are related to a certain field, it makes sense to have their button rendered within that field. See for instance: Reset_password_example ("Mot de passe" meaning "Password" in French).

I've managed to make the "Reset password" button appear in the password field by dedicating the help_text to it:

class UserChangeForm(admin_forms.UserChangeForm):
    class Meta(admin_forms.UserChangeForm.Meta):
        model = User

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        password = self.fields.get('password')
        if password:
            password.help_text = \
                """<ul class="object-tools"><li class="objectaction-item" data-tool-name="reset_password">
                <a href="/admin/users/guest/3/actions/reset_password/" title="" class="">Reset password</a>
                </li></ul>
                """

Very hacky, but it hopefully provides you with a useful starting point for implementing this feature, would you consider it.

Cheers