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 button, you will see that question has been published (you may have to refresh the page to see the results).
You can add as many actions that you need by separating them with commas.
Use the short_description
to set the label to display in the select-box.
If you need to iterate on all ...