3

I have a django admin action say "refresh", I want to add a refresh button for each row in the admin list view. I can create button using format_html but how can I invoke "refresh" action when it is pressed?

Ravi Kumar
  • 445
  • 4
  • 13

1 Answers1

4

I don't think admin actions are the best fit in this case.

Actions are

simple functions that get called with a list of objects selected on the change list page

With the buttons you describe however, you want to operate on a single row/instance. Therefore I would simply create a custom url endpoint for your ModelAdmin which is called when you press the button and which handles the desired action.

This article has quite a comprehensive overview how this can be done in detail.

arie
  • 18,737
  • 5
  • 70
  • 76
  • 1
    It works. I actually need it as a django action too. Button is good for one row because selecting one row then selecting action and clicking OK is three steps compared to clicking just one button. Thank You. – Ravi Kumar Mar 12 '17 at 15:18
  • There is a functionality called message_user which notifies when an admin action is finished. Is it possible to mimic this functionality using custom url? I am redirecting back to list page after clicking refresh. – Ravi Kumar Mar 12 '17 at 15:20
  • 1
    `message_user` seems to be simply a helper for the messages framework which can be used independently of admin actions: https://docs.djangoproject.com/en/1.10/ref/contrib/messages/#adding-a-message – arie Mar 12 '17 at 15:54