1. =============
    
  2. Admin actions
    
  3. =============
    
  4. 
    
  5. .. currentmodule:: django.contrib.admin
    
  6. 
    
  7. The basic workflow of Django's admin is, in a nutshell, "select an object,
    
  8. then change it." This works well for a majority of use cases. However, if you
    
  9. need to make the same change to many objects at once, this workflow can be
    
  10. quite tedious.
    
  11. 
    
  12. In these cases, Django's admin lets you write and register "actions" --
    
  13. functions that get called with a list of objects selected on the change list
    
  14. page.
    
  15. 
    
  16. If you look at any change list in the admin, you'll see this feature in
    
  17. action; Django ships with a "delete selected objects" action available to all
    
  18. models. For example, here's the user module from Django's built-in
    
  19. :mod:`django.contrib.auth` app:
    
  20. 
    
  21. .. image:: _images/admin-actions.png
    
  22. 
    
  23. .. warning::
    
  24. 
    
  25.     The "delete selected objects" action uses :meth:`QuerySet.delete()
    
  26.     <django.db.models.query.QuerySet.delete>` for efficiency reasons, which
    
  27.     has an important caveat: your model's ``delete()`` method will not be
    
  28.     called.
    
  29. 
    
  30.     If you wish to override this behavior, you can override
    
  31.     :meth:`.ModelAdmin.delete_queryset` or write a custom action which does
    
  32.     deletion in your preferred manner -- for example, by calling
    
  33.     ``Model.delete()`` for each of the selected items.
    
  34. 
    
  35.     For more background on bulk deletion, see the documentation on :ref:`object
    
  36.     deletion <topics-db-queries-delete>`.
    
  37. 
    
  38. Read on to find out how to add your own actions to this list.
    
  39. 
    
  40. Writing actions
    
  41. ===============
    
  42. 
    
  43. The easiest way to explain actions is by example, so let's dive in.
    
  44. 
    
  45. A common use case for admin actions is the bulk updating of a model. Imagine a
    
  46. news application with an ``Article`` model::
    
  47. 
    
  48.     from django.db import models
    
  49. 
    
  50.     STATUS_CHOICES = [
    
  51.         ('d', 'Draft'),
    
  52.         ('p', 'Published'),
    
  53.         ('w', 'Withdrawn'),
    
  54.     ]
    
  55. 
    
  56.     class Article(models.Model):
    
  57.         title = models.CharField(max_length=100)
    
  58.         body = models.TextField()
    
  59.         status = models.CharField(max_length=1, choices=STATUS_CHOICES)
    
  60. 
    
  61.         def __str__(self):
    
  62.             return self.title
    
  63. 
    
  64. A common task we might perform with a model like this is to update an
    
  65. article's status from "draft" to "published". We could easily do this in the
    
  66. admin one article at a time, but if we wanted to bulk-publish a group of
    
  67. articles, it'd be tedious. So, let's write an action that lets us change an
    
  68. article's status to "published."
    
  69. 
    
  70. Writing action functions
    
  71. ------------------------
    
  72. 
    
  73. First, we'll need to write a function that gets called when the action is
    
  74. triggered from the admin. Action functions are regular functions that take
    
  75. three arguments:
    
  76. 
    
  77. * The current :class:`ModelAdmin`
    
  78. * An :class:`~django.http.HttpRequest` representing the current request,
    
  79. * A :class:`~django.db.models.query.QuerySet` containing the set of
    
  80.   objects selected by the user.
    
  81. 
    
  82. Our publish-these-articles function won't need the :class:`ModelAdmin` or the
    
  83. request object, but we will use the queryset::
    
  84. 
    
  85.     def make_published(modeladmin, request, queryset):
    
  86.         queryset.update(status='p')
    
  87. 
    
  88. .. note::
    
  89. 
    
  90.     For the best performance, we're using the queryset's :ref:`update method
    
  91.     <topics-db-queries-update>`. Other types of actions might need to deal
    
  92.     with each object individually; in these cases we'd iterate over the
    
  93.     queryset::
    
  94. 
    
  95.         for obj in queryset:
    
  96.             do_something_with(obj)
    
  97. 
    
  98. That's actually all there is to writing an action! However, we'll take one
    
  99. more optional-but-useful step and give the action a "nice" title in the admin.
    
  100. By default, this action would appear in the action list as "Make published" --
    
  101. the function name, with underscores replaced by spaces. That's fine, but we
    
  102. can provide a better, more human-friendly name by using the
    
  103. :func:`~django.contrib.admin.action` decorator on the ``make_published``
    
  104. function::
    
  105. 
    
  106.     from django.contrib import admin
    
  107. 
    
  108.     ...
    
  109. 
    
  110.     @admin.action(description='Mark selected stories as published')
    
  111.     def make_published(modeladmin, request, queryset):
    
  112.         queryset.update(status='p')
    
  113. 
    
  114. .. note::
    
  115. 
    
  116.     This might look familiar; the admin's
    
  117.     :attr:`~django.contrib.admin.ModelAdmin.list_display` option uses a similar
    
  118.     technique with the :func:`~django.contrib.admin.display` decorator to
    
  119.     provide human-readable descriptions for callback functions registered
    
  120.     there, too.
    
  121. 
    
  122. Adding actions to the :class:`ModelAdmin`
    
  123. -----------------------------------------
    
  124. 
    
  125. Next, we'll need to inform our :class:`ModelAdmin` of the action. This works
    
  126. just like any other configuration option. So, the complete ``admin.py`` with
    
  127. the action and its registration would look like::
    
  128. 
    
  129.     from django.contrib import admin
    
  130.     from myapp.models import Article
    
  131. 
    
  132.     @admin.action(description='Mark selected stories as published')
    
  133.     def make_published(modeladmin, request, queryset):
    
  134.         queryset.update(status='p')
    
  135. 
    
  136.     class ArticleAdmin(admin.ModelAdmin):
    
  137.         list_display = ['title', 'status']
    
  138.         ordering = ['title']
    
  139.         actions = [make_published]
    
  140. 
    
  141.     admin.site.register(Article, ArticleAdmin)
    
  142. 
    
  143. That code will give us an admin change list that looks something like this:
    
  144. 
    
  145. .. image:: _images/adding-actions-to-the-modeladmin.png
    
  146. 
    
  147. That's really all there is to it! If you're itching to write your own actions,
    
  148. you now know enough to get started. The rest of this document covers more
    
  149. advanced techniques.
    
  150. 
    
  151. Handling errors in actions
    
  152. --------------------------
    
  153. 
    
  154. If there are foreseeable error conditions that may occur while running your
    
  155. action, you should gracefully inform the user of the problem. This means
    
  156. handling exceptions and using
    
  157. :meth:`django.contrib.admin.ModelAdmin.message_user` to display a user friendly
    
  158. description of the problem in the response.
    
  159. 
    
  160. Advanced action techniques
    
  161. ==========================
    
  162. 
    
  163. There's a couple of extra options and possibilities you can exploit for more
    
  164. advanced options.
    
  165. 
    
  166. Actions as :class:`ModelAdmin` methods
    
  167. --------------------------------------
    
  168. 
    
  169. The example above shows the ``make_published`` action defined as a function.
    
  170. That's perfectly fine, but it's not perfect from a code design point of view:
    
  171. since the action is tightly coupled to the ``Article`` object, it makes sense
    
  172. to hook the action to the ``ArticleAdmin`` object itself.
    
  173. 
    
  174. You can do it like this::
    
  175. 
    
  176.     class ArticleAdmin(admin.ModelAdmin):
    
  177.         ...
    
  178. 
    
  179.         actions = ['make_published']
    
  180. 
    
  181.         @admin.action(description='Mark selected stories as published')
    
  182.         def make_published(self, request, queryset):
    
  183.             queryset.update(status='p')
    
  184. 
    
  185. Notice first that we've moved ``make_published`` into a method and renamed the
    
  186. ``modeladmin`` parameter to ``self``, and second that we've now put the string
    
  187. ``'make_published'`` in ``actions`` instead of a direct function reference. This
    
  188. tells the :class:`ModelAdmin` to look up the action as a method.
    
  189. 
    
  190. Defining actions as methods gives the action more idiomatic access to the
    
  191. :class:`ModelAdmin` itself, allowing the action to call any of the methods
    
  192. provided by the admin.
    
  193. 
    
  194. .. _custom-admin-action:
    
  195. 
    
  196. For example, we can use ``self`` to flash a message to the user informing them
    
  197. that the action was successful::
    
  198. 
    
  199.     from django.contrib import messages
    
  200.     from django.utils.translation import ngettext
    
  201. 
    
  202.     class ArticleAdmin(admin.ModelAdmin):
    
  203.         ...
    
  204. 
    
  205.         def make_published(self, request, queryset):
    
  206.             updated = queryset.update(status='p')
    
  207.             self.message_user(request, ngettext(
    
  208.                 '%d story was successfully marked as published.',
    
  209.                 '%d stories were successfully marked as published.',
    
  210.                 updated,
    
  211.             ) % updated, messages.SUCCESS)
    
  212. 
    
  213. This make the action match what the admin itself does after successfully
    
  214. performing an action:
    
  215. 
    
  216. .. image:: _images/actions-as-modeladmin-methods.png
    
  217. 
    
  218. Actions that provide intermediate pages
    
  219. ---------------------------------------
    
  220. 
    
  221. By default, after an action is performed the user is redirected back to the
    
  222. original change list page. However, some actions, especially more complex ones,
    
  223. will need to return intermediate pages. For example, the built-in delete action
    
  224. asks for confirmation before deleting the selected objects.
    
  225. 
    
  226. To provide an intermediary page, return an :class:`~django.http.HttpResponse`
    
  227. (or subclass) from your action. For example, you might write an export function
    
  228. that uses Django's :doc:`serialization functions </topics/serialization>` to
    
  229. dump some selected objects as JSON::
    
  230. 
    
  231.     from django.core import serializers
    
  232.     from django.http import HttpResponse
    
  233. 
    
  234.     def export_as_json(modeladmin, request, queryset):
    
  235.         response = HttpResponse(content_type="application/json")
    
  236.         serializers.serialize("json", queryset, stream=response)
    
  237.         return response
    
  238. 
    
  239. Generally, something like the above isn't considered a great idea. Most of the
    
  240. time, the best practice will be to return an
    
  241. :class:`~django.http.HttpResponseRedirect` and redirect the user to a view
    
  242. you've written, passing the list of selected objects in the GET query string.
    
  243. This allows you to provide complex interaction logic on the intermediary
    
  244. pages. For example, if you wanted to provide a more complete export function,
    
  245. you'd want to let the user choose a format, and possibly a list of fields to
    
  246. include in the export. The best thing to do would be to write a small action
    
  247. that redirects to your custom export view::
    
  248. 
    
  249.     from django.contrib.contenttypes.models import ContentType
    
  250.     from django.http import HttpResponseRedirect
    
  251. 
    
  252.     def export_selected_objects(modeladmin, request, queryset):
    
  253.         selected = queryset.values_list('pk', flat=True)
    
  254.         ct = ContentType.objects.get_for_model(queryset.model)
    
  255.         return HttpResponseRedirect('/export/?ct=%s&ids=%s' % (
    
  256.             ct.pk,
    
  257.             ','.join(str(pk) for pk in selected),
    
  258.         ))
    
  259. 
    
  260. As you can see, the action is rather short; all the complex logic would belong
    
  261. in your export view. This would need to deal with objects of any type, hence
    
  262. the business with the ``ContentType``.
    
  263. 
    
  264. Writing this view is left as an exercise to the reader.
    
  265. 
    
  266. .. _adminsite-actions:
    
  267. 
    
  268. Making actions available site-wide
    
  269. ----------------------------------
    
  270. 
    
  271. .. method:: AdminSite.add_action(action, name=None)
    
  272. 
    
  273.     Some actions are best if they're made available to *any* object in the admin
    
  274.     site -- the export action defined above would be a good candidate. You can
    
  275.     make an action globally available using :meth:`AdminSite.add_action()`. For
    
  276.     example::
    
  277. 
    
  278.         from django.contrib import admin
    
  279. 
    
  280.         admin.site.add_action(export_selected_objects)
    
  281. 
    
  282.     This makes the ``export_selected_objects`` action globally available as an
    
  283.     action named "export_selected_objects". You can explicitly give the action
    
  284.     a name -- good if you later want to programmatically :ref:`remove the action
    
  285.     <disabling-admin-actions>` -- by passing a second argument to
    
  286.     :meth:`AdminSite.add_action()`::
    
  287. 
    
  288.         admin.site.add_action(export_selected_objects, 'export_selected')
    
  289. 
    
  290. .. _disabling-admin-actions:
    
  291. 
    
  292. Disabling actions
    
  293. -----------------
    
  294. 
    
  295. Sometimes you need to disable certain actions -- especially those
    
  296. :ref:`registered site-wide <adminsite-actions>` -- for particular objects.
    
  297. There's a few ways you can disable actions:
    
  298. 
    
  299. Disabling a site-wide action
    
  300. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  301. 
    
  302. .. method:: AdminSite.disable_action(name)
    
  303. 
    
  304.     If you need to disable a :ref:`site-wide action <adminsite-actions>` you can
    
  305.     call :meth:`AdminSite.disable_action()`.
    
  306. 
    
  307.     For example, you can use this method to remove the built-in "delete selected
    
  308.     objects" action::
    
  309. 
    
  310.         admin.site.disable_action('delete_selected')
    
  311. 
    
  312.     Once you've done the above, that action will no longer be available
    
  313.     site-wide.
    
  314. 
    
  315.     If, however, you need to reenable a globally-disabled action for one
    
  316.     particular model, list it explicitly in your ``ModelAdmin.actions`` list::
    
  317. 
    
  318.         # Globally disable delete selected
    
  319.         admin.site.disable_action('delete_selected')
    
  320. 
    
  321.         # This ModelAdmin will not have delete_selected available
    
  322.         class SomeModelAdmin(admin.ModelAdmin):
    
  323.             actions = ['some_other_action']
    
  324.             ...
    
  325. 
    
  326.         # This one will
    
  327.         class AnotherModelAdmin(admin.ModelAdmin):
    
  328.             actions = ['delete_selected', 'a_third_action']
    
  329.             ...
    
  330. 
    
  331. 
    
  332. Disabling all actions for a particular :class:`ModelAdmin`
    
  333. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  334. 
    
  335. If you want *no* bulk actions available for a given :class:`ModelAdmin`, set
    
  336. :attr:`ModelAdmin.actions` to ``None``::
    
  337. 
    
  338.     class MyModelAdmin(admin.ModelAdmin):
    
  339.         actions = None
    
  340. 
    
  341. This tells the :class:`ModelAdmin` to not display or allow any actions,
    
  342. including any :ref:`site-wide actions <adminsite-actions>`.
    
  343. 
    
  344. Conditionally enabling or disabling actions
    
  345. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  346. 
    
  347. .. method:: ModelAdmin.get_actions(request)
    
  348. 
    
  349.     Finally, you can conditionally enable or disable actions on a per-request
    
  350.     (and hence per-user basis) by overriding :meth:`ModelAdmin.get_actions`.
    
  351. 
    
  352.     This returns a dictionary of actions allowed. The keys are action names, and
    
  353.     the values are ``(function, name, short_description)`` tuples.
    
  354. 
    
  355.     For example, if you only want users whose names begin with 'J' to be able
    
  356.     to delete objects in bulk::
    
  357. 
    
  358.         class MyModelAdmin(admin.ModelAdmin):
    
  359.             ...
    
  360. 
    
  361.             def get_actions(self, request):
    
  362.                 actions = super().get_actions(request)
    
  363.                 if request.user.username[0].upper() != 'J':
    
  364.                     if 'delete_selected' in actions:
    
  365.                         del actions['delete_selected']
    
  366.                 return actions
    
  367. 
    
  368. .. _admin-action-permissions:
    
  369. 
    
  370. Setting permissions for actions
    
  371. -------------------------------
    
  372. 
    
  373. Actions may limit their availability to users with specific permissions by
    
  374. wrapping the action function with the :func:`~django.contrib.admin.action`
    
  375. decorator and passing the ``permissions`` argument::
    
  376. 
    
  377.     @admin.action(permissions=['change'])
    
  378.     def make_published(modeladmin, request, queryset):
    
  379.         queryset.update(status='p')
    
  380. 
    
  381. The ``make_published()`` action will only be available to users that pass the
    
  382. :meth:`.ModelAdmin.has_change_permission` check.
    
  383. 
    
  384. If ``permissions`` has more than one permission, the action will be available
    
  385. as long as the user passes at least one of the checks.
    
  386. 
    
  387. Available values for ``permissions`` and the corresponding method checks are:
    
  388. 
    
  389. - ``'add'``: :meth:`.ModelAdmin.has_add_permission`
    
  390. - ``'change'``: :meth:`.ModelAdmin.has_change_permission`
    
  391. - ``'delete'``: :meth:`.ModelAdmin.has_delete_permission`
    
  392. - ``'view'``: :meth:`.ModelAdmin.has_view_permission`
    
  393. 
    
  394. You can specify any other value as long as you implement a corresponding
    
  395. ``has_<value>_permission(self, request)`` method on the ``ModelAdmin``.
    
  396. 
    
  397. For example::
    
  398. 
    
  399.     from django.contrib import admin
    
  400.     from django.contrib.auth import get_permission_codename
    
  401. 
    
  402.     class ArticleAdmin(admin.ModelAdmin):
    
  403.         actions = ['make_published']
    
  404. 
    
  405.         @admin.action(permissions=['publish'])
    
  406.         def make_published(self, request, queryset):
    
  407.             queryset.update(status='p')
    
  408. 
    
  409.         def has_publish_permission(self, request):
    
  410.             """Does the user have the publish permission?"""
    
  411.             opts = self.opts
    
  412.             codename = get_permission_codename('publish', opts)
    
  413.             return request.user.has_perm('%s.%s' % (opts.app_label, codename))
    
  414. 
    
  415. The ``action`` decorator
    
  416. ========================
    
  417. 
    
  418. .. function:: action(*, permissions=None, description=None)
    
  419. 
    
  420.     This decorator can be used for setting specific attributes on custom action
    
  421.     functions that can be used with
    
  422.     :attr:`~django.contrib.admin.ModelAdmin.actions`::
    
  423. 
    
  424.         @admin.action(
    
  425.             permissions=['publish'],
    
  426.             description='Mark selected stories as published',
    
  427.         )
    
  428.         def make_published(self, request, queryset):
    
  429.             queryset.update(status='p')
    
  430. 
    
  431.     This is equivalent to setting some attributes (with the original, longer
    
  432.     names) on the function directly::
    
  433. 
    
  434.         def make_published(self, request, queryset):
    
  435.             queryset.update(status='p')
    
  436.         make_published.allowed_permissions = ['publish']
    
  437.         make_published.short_description = 'Mark selected stories as published'
    
  438. 
    
  439.     Use of this decorator is not compulsory to make an action function, but it
    
  440.     can be useful to use it without arguments as a marker in your source to
    
  441.     identify the purpose of the function::
    
  442. 
    
  443.         @admin.action
    
  444.         def make_inactive(self, request, queryset):
    
  445.             queryset.update(is_active=False)
    
  446. 
    
  447.     In this case it will add no attributes to the function.