Add new action in changelist in Django

Django ship a default action “Delete selected <objects>”, how can we add a new action? It is easy to do that. All you need to do are in admin.py where the model admins are registered.

First write your action handler function like this

def tasks_finished( modeladmin, request, queryset ):
    queryset.update( finished = True )
tasks_finished.short_description = "Mark these tasks as finished"

Then register the handler in your model admin site

class TaskAdmin( admin.ModelAdmin ):
    list_display = [ "project", "title", "user", "works", "time", "finished" ]
    list_filter = [ "finished", "user", "project" ]
    inlines = [ WorkAdminInline ]
    ordering = [ "project", "finished", "user", "title" ]
    actions = [ tasks_finished ]

That is all you need to do.

This entry was posted in Django and tagged , . Bookmark the permalink.


3 Responses to Add new action in changelist in Django

  1. This blog appears to get a great deal of visitors. How do you get traffic to it? It offers a nice individual twist on things. I guess having something real or substantial to talk about is the most important thing.

  2. admin says:


  3. trying to follow you on twitter but cant find your name

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>