Custom Actions In Django Admin Object Editor
I’ve seen many posts asking for the simplest feature in Django admin… the ability to add custom actions next to History and View On Site in the Django admin form. The page where the actual object is edited, not the list pages. Imagine adding actions like:
- Edit Next Item
- Edit Previous Item
- Send Thank You Email
- Export Record As CSV
Thats just the tip of the iceberg. Basically any action can be performed. The concept is simple, create an buton for an action, redirect to a handler with model and id of object, execute any code, then redirect back to the admin page. This would turn Django admin into any application the developer chooses.
I first wrote on this topic in Adding Custom Actions To Django Admin Change Forms, back when Django was in version 0.95. I’m writing this post again to document the code port from 0.96 to 0.96 and above.
Changes To Django
First add code to Django to facilitate custom actions. Working backwards from the template is the simplest way to visualize the changes.
Step One: Add a list of custom actions to the Django admin change form template. I added the for loop…
contrib/admin/templates/admin/change_form.html
You may have noticed that the custom action will call a URL of the format /admin/handler/model/id/. I think its simple and provides enough information to identify the object being edited in all circumstances. There may be some overlap with admin URLs so please be careful or change the URL here.
Step Two: Add a member to the Django admin change form handler to be passed into the template. This is just a one liner, add form_action to the context dictionary.
contrib/admin/views/main.py
Step Three: Add the new custom form_actions field to the Admin class. You’re really adding it to the AdminOptions class, but you get the picture. I added the form_actions to the function signature and the list of member variables.
db/models/options.py
Those three files is all you have to edit. Now you have support for custom actions. I assume you’re OK with the custom action protocol I implemented above. If you want to change it then I leave the edits up to you. If you take me at my word, then all you have to do is add the proper code to your models file and your URL handlers.
Your Custom Handler
All the pieces are in place on the Django admin side. Now to create a custom handler.
Step One: Add a list of actions to your model definition under the Admin class.
models.py
If you refresh your Admin page here you will see the new custom action.
Step Two: Create a handler for your new action in the URL handler. Remember if you use admin as your prefix then place the handler regexp before the one for Django admin.
urls.py
You’re done. Add as many custom actions as please you. I recommend redirecting back to the Django admin page after executing each action. You have the model id and the object id, you can even create generic handlers for any object. In my case I typically only use the object id, each handler is model specific in my case.
Request To Readers
I would really like it if this feature was included in the official Django release. Please leave a comment in support of this code. I would like to approach the Django maintenance group and petition a feature request based on your comments. The more comments in support, the more likely we will see this in the official Django version.
More from Aware Labs
- Everything A Django Developer Needs To Create Logins
- Goodbye WebFaction Django Hosting – A Reflection
- When Django Apps Grow Up
- Implementing CNN style votes in Django, Episode I
- Implementing CNN Style Votes In Django, Episode II
Aware Labs Recommends
- Popularizing Django — Or Reusable apps considered harmful. (USwaretech)
- Django’s tipping point (Antonio Cangiano)
- An Interview with Jacob Kaplan-Moss – Creator of Django (USwaretech)
-
sclaughl
-
sclaughl
-
tomovo
-
Gert Steyn
-
Andreas E
-
Lars Holm Nielsen
-
Waylan

![Recommend [AwareLabs]](http://s3.amazonaws.com/arkayne-media/img/badge/logo-recommend-badge-medium.png)