=============Admin actions=============.. currentmodule:: django.contrib.adminThe basic workflow of Django's admin is, in a nutshell, "select an object,then change it." This works well for a majority of use cases. However, if youneed to make the same change to many objects at once, this workflow can bequite tedious.In these cases, Django's admin lets you write and register "actions" --functions that get called with a list of objects selected on the change listpage.If you look at any change list in the admin, you'll see this feature inaction; Django ships with a "delete selected objects" action available to allmodels. For example, here's the user module from Django's built-in:mod:`django.contrib.auth` app:.. image:: _images/admin-actions.png.. warning::The "delete selected objects" action uses :meth:`QuerySet.delete()<django.db.models.query.QuerySet.delete>` for efficiency reasons, whichhas an important caveat: your model's ``delete()`` method will not becalled.If you wish to override this behavior, you can override:meth:`.ModelAdmin.delete_queryset` or write a custom action which doesdeletion in your preferred manner -- for example, by calling``Model.delete()`` for each of the selected items.For more background on bulk deletion, see the documentation on :ref:`objectdeletion <topics-db-queries-delete>`.Read on to find out how to add your own actions to this list.Writing actions===============The easiest way to explain actions is by example, so let's dive in.A common use case for admin actions is the bulk updating of a model. Imagine anews application with an ``Article`` model::from django.db import modelsSTATUS_CHOICES = [('d', 'Draft'),('p', 'Published'),('w', 'Withdrawn'),]class Article(models.Model):title = models.CharField(max_length=100)body = models.TextField()status = models.CharField(max_length=1, choices=STATUS_CHOICES)def __str__(self):return self.titleA common task we might perform with a model like this is to update anarticle's status from "draft" to "published". We could easily do this in theadmin one article at a time, but if we wanted to bulk-publish a group ofarticles, it'd be tedious. So, let's write an action that lets us change anarticle's status to "published."Writing action functions------------------------First, we'll need to write a function that gets called when the action istriggered from the admin. Action functions are regular functions that takethree arguments:* The current :class:`ModelAdmin`* An :class:`~django.http.HttpRequest` representing the current request,* A :class:`~django.db.models.query.QuerySet` containing the set ofobjects selected by the user.Our publish-these-articles function won't need the :class:`ModelAdmin` or therequest object, but we will use the queryset::def make_published(modeladmin, request, queryset):queryset.update(status='p').. note::For the best performance, we're using the queryset's :ref:`update method<topics-db-queries-update>`. Other types of actions might need to dealwith each object individually; in these cases we'd iterate over thequeryset::for obj in queryset:do_something_with(obj)That's actually all there is to writing an action! However, we'll take onemore optional-but-useful step and give the action a "nice" title in the admin.By default, this action would appear in the action list as "Make published" --the function name, with underscores replaced by spaces. That's fine, but wecan provide a better, more human-friendly name by using the:func:`~django.contrib.admin.action` decorator on the ``make_published``function::from django.contrib import admin...@admin.action(description='Mark selected stories as published')def make_published(modeladmin, request, queryset):queryset.update(status='p').. note::This might look familiar; the admin's:attr:`~django.contrib.admin.ModelAdmin.list_display` option uses a similartechnique with the :func:`~django.contrib.admin.display` decorator toprovide human-readable descriptions for callback functions registeredthere, too.Adding actions to the :class:`ModelAdmin`-----------------------------------------Next, we'll need to inform our :class:`ModelAdmin` of the action. This worksjust like any other configuration option. So, the complete ``admin.py`` withthe action and its registration would look like::from django.contrib import adminfrom myapp.models import Article@admin.action(description='Mark selected stories as published')def make_published(modeladmin, request, queryset):queryset.update(status='p')class ArticleAdmin(admin.ModelAdmin):list_display = ['title', 'status']ordering = ['title']actions = [make_published]admin.site.register(Article, ArticleAdmin)That code will give us an admin change list that looks something like this:.. image:: _images/adding-actions-to-the-modeladmin.pngThat's really all there is to it! If you're itching to write your own actions,you now know enough to get started. The rest of this document covers moreadvanced techniques.Handling errors in actions--------------------------If there are foreseeable error conditions that may occur while running youraction, you should gracefully inform the user of the problem. This meanshandling exceptions and using:meth:`django.contrib.admin.ModelAdmin.message_user` to display a user friendlydescription of the problem in the response.Advanced action techniques==========================There's a couple of extra options and possibilities you can exploit for moreadvanced options.Actions as :class:`ModelAdmin` methods--------------------------------------The example above shows the ``make_published`` action defined as a function.That's perfectly fine, but it's not perfect from a code design point of view:since the action is tightly coupled to the ``Article`` object, it makes senseto hook the action to the ``ArticleAdmin`` object itself.You can do it like this::class ArticleAdmin(admin.ModelAdmin):...actions = ['make_published']@admin.action(description='Mark selected stories as published')def make_published(self, request, queryset):queryset.update(status='p')Notice first that we've moved ``make_published`` into a method and renamed the``modeladmin`` parameter to ``self``, and second that we've now put the string``'make_published'`` in ``actions`` instead of a direct function reference. Thistells the :class:`ModelAdmin` to look up the action as a method.Defining actions as methods gives the action more idiomatic access to the:class:`ModelAdmin` itself, allowing the action to call any of the methodsprovided by the admin... _custom-admin-action:For example, we can use ``self`` to flash a message to the user informing themthat the action was successful::from django.contrib import messagesfrom django.utils.translation import ngettextclass ArticleAdmin(admin.ModelAdmin):...def make_published(self, request, queryset):updated = queryset.update(status='p')self.message_user(request, ngettext('%d story was successfully marked as published.','%d stories were successfully marked as published.',updated,) % updated, messages.SUCCESS)This make the action match what the admin itself does after successfullyperforming an action:.. image:: _images/actions-as-modeladmin-methods.pngActions that provide intermediate pages---------------------------------------By default, after an action is performed the user is redirected back to theoriginal change list page. However, some actions, especially more complex ones,will need to return intermediate pages. For example, the built-in delete actionasks for confirmation before deleting the selected objects.To provide an intermediary page, return an :class:`~django.http.HttpResponse`(or subclass) from your action. For example, you might write an export functionthat uses Django's :doc:`serialization functions </topics/serialization>` todump some selected objects as JSON::from django.core import serializersfrom django.http import HttpResponsedef export_as_json(modeladmin, request, queryset):response = HttpResponse(content_type="application/json")serializers.serialize("json", queryset, stream=response)return responseGenerally, something like the above isn't considered a great idea. Most of thetime, the best practice will be to return an:class:`~django.http.HttpResponseRedirect` and redirect the user to a viewyou've written, passing the list of selected objects in the GET query string.This allows you to provide complex interaction logic on the intermediarypages. For example, if you wanted to provide a more complete export function,you'd want to let the user choose a format, and possibly a list of fields toinclude in the export. The best thing to do would be to write a small actionthat redirects to your custom export view::from django.contrib.contenttypes.models import ContentTypefrom django.http import HttpResponseRedirectdef export_selected_objects(modeladmin, request, queryset):selected = queryset.values_list('pk', flat=True)ct = ContentType.objects.get_for_model(queryset.model)return HttpResponseRedirect('/export/?ct=%s&ids=%s' % (ct.pk,','.join(str(pk) for pk in selected),))As you can see, the action is rather short; all the complex logic would belongin your export view. This would need to deal with objects of any type, hencethe business with the ``ContentType``.Writing this view is left as an exercise to the reader... _adminsite-actions:Making actions available site-wide----------------------------------.. method:: AdminSite.add_action(action, name=None)Some actions are best if they're made available to *any* object in the adminsite -- the export action defined above would be a good candidate. You canmake an action globally available using :meth:`AdminSite.add_action()`. Forexample::from django.contrib import adminadmin.site.add_action(export_selected_objects)This makes the ``export_selected_objects`` action globally available as anaction named "export_selected_objects". You can explicitly give the actiona name -- good if you later want to programmatically :ref:`remove the action<disabling-admin-actions>` -- by passing a second argument to:meth:`AdminSite.add_action()`::admin.site.add_action(export_selected_objects, 'export_selected').. _disabling-admin-actions:Disabling actions-----------------Sometimes you need to disable certain actions -- especially those:ref:`registered site-wide <adminsite-actions>` -- for particular objects.There's a few ways you can disable actions:Disabling a site-wide action~~~~~~~~~~~~~~~~~~~~~~~~~~~~.. method:: AdminSite.disable_action(name)If you need to disable a :ref:`site-wide action <adminsite-actions>` you cancall :meth:`AdminSite.disable_action()`.For example, you can use this method to remove the built-in "delete selectedobjects" action::admin.site.disable_action('delete_selected')Once you've done the above, that action will no longer be availablesite-wide.If, however, you need to reenable a globally-disabled action for oneparticular model, list it explicitly in your ``ModelAdmin.actions`` list::# Globally disable delete selectedadmin.site.disable_action('delete_selected')# This ModelAdmin will not have delete_selected availableclass SomeModelAdmin(admin.ModelAdmin):actions = ['some_other_action']...# This one willclass AnotherModelAdmin(admin.ModelAdmin):actions = ['delete_selected', 'a_third_action']...Disabling all actions for a particular :class:`ModelAdmin`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~If you want *no* bulk actions available for a given :class:`ModelAdmin`, set:attr:`ModelAdmin.actions` to ``None``::class MyModelAdmin(admin.ModelAdmin):actions = NoneThis tells the :class:`ModelAdmin` to not display or allow any actions,including any :ref:`site-wide actions <adminsite-actions>`.Conditionally enabling or disabling actions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.. method:: ModelAdmin.get_actions(request)Finally, you can conditionally enable or disable actions on a per-request(and hence per-user basis) by overriding :meth:`ModelAdmin.get_actions`.This returns a dictionary of actions allowed. The keys are action names, andthe values are ``(function, name, short_description)`` tuples.For example, if you only want users whose names begin with 'J' to be ableto delete objects in bulk::class MyModelAdmin(admin.ModelAdmin):...def get_actions(self, request):actions = super().get_actions(request)if request.user.username[0].upper() != 'J':if 'delete_selected' in actions:del actions['delete_selected']return actions.. _admin-action-permissions:Setting permissions for actions-------------------------------Actions may limit their availability to users with specific permissions bywrapping the action function with the :func:`~django.contrib.admin.action`decorator and passing the ``permissions`` argument::@admin.action(permissions=['change'])def make_published(modeladmin, request, queryset):queryset.update(status='p')The ``make_published()`` action will only be available to users that pass the:meth:`.ModelAdmin.has_change_permission` check.If ``permissions`` has more than one permission, the action will be availableas long as the user passes at least one of the checks.Available values for ``permissions`` and the corresponding method checks are:- ``'add'``: :meth:`.ModelAdmin.has_add_permission`- ``'change'``: :meth:`.ModelAdmin.has_change_permission`- ``'delete'``: :meth:`.ModelAdmin.has_delete_permission`- ``'view'``: :meth:`.ModelAdmin.has_view_permission`You can specify any other value as long as you implement a corresponding``has_<value>_permission(self, request)`` method on the ``ModelAdmin``.For example::from django.contrib import adminfrom django.contrib.auth import get_permission_codenameclass ArticleAdmin(admin.ModelAdmin):actions = ['make_published']@admin.action(permissions=['publish'])def make_published(self, request, queryset):queryset.update(status='p')def has_publish_permission(self, request):"""Does the user have the publish permission?"""opts = self.optscodename = get_permission_codename('publish', opts)return request.user.has_perm('%s.%s' % (opts.app_label, codename))The ``action`` decorator========================.. function:: action(*, permissions=None, description=None)This decorator can be used for setting specific attributes on custom actionfunctions that can be used with:attr:`~django.contrib.admin.ModelAdmin.actions`::@admin.action(permissions=['publish'],description='Mark selected stories as published',)def make_published(self, request, queryset):queryset.update(status='p')This is equivalent to setting some attributes (with the original, longernames) on the function directly::def make_published(self, request, queryset):queryset.update(status='p')make_published.allowed_permissions = ['publish']make_published.short_description = 'Mark selected stories as published'Use of this decorator is not compulsory to make an action function, but itcan be useful to use it without arguments as a marker in your source toidentify the purpose of the function::@admin.actiondef make_inactive(self, request, queryset):queryset.update(is_active=False)In this case it will add no attributes to the function.