Custom Actions
Learn about custom actions and how to add them to your Django change view page.
We'll cover the following...
Adding custom actions
On the list pages, Django admin provides an action select-box to delete results.
You can add your custom actions. For example, you will add a publish items action.
def make_published(modeladmin, request, queryset):
queryset.update(pub_date=datetime.now()- timedelta(days=1))
make_published.short_description = "Mark selected questions as published"
You need to create a function with your business logic code. A question is displayed as published when its pub_date
is in the past, so you will subtract one day from the current date. This will make the current question appear as published and display a tick or the word True against it in the row.
Then, to add the new action in the list of the admin page, you will just write:
actions = [make_published]
Now, if you select the questions and click on the GO ...