Last active
July 23, 2024 04:43
-
-
Save cassus/9757084 to your computer and use it in GitHub Desktop.
Django admin action as row button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyAdmin(admin.ModelAdmin): | |
list_display = (..., 'actions_html') | |
def actions_html(self, obj): | |
return format_html('<button class="btn" type="button" onclick="activate_and_send_email({pk})">Activate and send email</button>', pk=obj.pk) | |
actions_html.allow_tags = True | |
actions_html.short_description = "Actions" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
activate_and_send_email = function(pk) { | |
$('input[name="_selected_action"]').removeAttr('checked'); | |
$('input[name="_selected_action"][value="{}"]'.replace('{}', pk)).attr('checked', 'checked'); | |
$('select[name="action"]').val('activate_account_and_send_email'); | |
$('button[type="submit"][name="index"]').click(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great ! One question : Is there a way to clear the corresponding row checkbox after finishing the action ? Thank you !