actions.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. =============
  2. Admin actions
  3. =============
  4. .. currentmodule:: django.contrib.admin
  5. The basic workflow of Django's admin is, in a nutshell, "select an object,
  6. then change it." This works well for a majority of use cases. However, if you
  7. need to make the same change to many objects at once, this workflow can be
  8. quite tedious.
  9. In these cases, Django's admin lets you write and register "actions" -- simple
  10. functions that get called with a list of objects selected on the change list
  11. page.
  12. If you look at any change list in the admin, you'll see this feature in
  13. action; Django ships with a "delete selected objects" action available to all
  14. models. For example, here's the user module from Django's built-in
  15. :mod:`django.contrib.auth` app:
  16. .. image:: _images/admin-actions.png
  17. .. warning::
  18. The "delete selected objects" action uses :meth:`QuerySet.delete()
  19. <django.db.models.query.QuerySet.delete>` for efficiency reasons, which
  20. has an important caveat: your model's ``delete()`` method will not be
  21. called.
  22. If you wish to override this behavior, simply write a custom action which
  23. accomplishes deletion in your preferred manner -- for example, by calling
  24. ``Model.delete()`` for each of the selected items.
  25. For more background on bulk deletion, see the documentation on :ref:`object
  26. deletion <topics-db-queries-delete>`.
  27. Read on to find out how to add your own actions to this list.
  28. Writing actions
  29. ===============
  30. The easiest way to explain actions is by example, so let's dive in.
  31. A common use case for admin actions is the bulk updating of a model. Imagine a
  32. simple news application with an ``Article`` model::
  33. from django.db import models
  34. STATUS_CHOICES = (
  35. ('d', 'Draft'),
  36. ('p', 'Published'),
  37. ('w', 'Withdrawn'),
  38. )
  39. class Article(models.Model):
  40. title = models.CharField(max_length=100)
  41. body = models.TextField()
  42. status = models.CharField(max_length=1, choices=STATUS_CHOICES)
  43. def __str__(self):
  44. return self.title
  45. A common task we might perform with a model like this is to update an
  46. article's status from "draft" to "published". We could easily do this in the
  47. admin one article at a time, but if we wanted to bulk-publish a group of
  48. articles, it'd be tedious. So, let's write an action that lets us change an
  49. article's status to "published."
  50. Writing action functions
  51. ------------------------
  52. First, we'll need to write a function that gets called when the action is
  53. triggered from the admin. Action functions are just regular functions that take
  54. three arguments:
  55. * The current :class:`ModelAdmin`
  56. * An :class:`~django.http.HttpRequest` representing the current request,
  57. * A :class:`~django.db.models.query.QuerySet` containing the set of
  58. objects selected by the user.
  59. Our publish-these-articles function won't need the :class:`ModelAdmin` or the
  60. request object, but we will use the queryset::
  61. def make_published(modeladmin, request, queryset):
  62. queryset.update(status='p')
  63. .. note::
  64. For the best performance, we're using the queryset's :ref:`update method
  65. <topics-db-queries-update>`. Other types of actions might need to deal
  66. with each object individually; in these cases we'd just iterate over the
  67. queryset::
  68. for obj in queryset:
  69. do_something_with(obj)
  70. That's actually all there is to writing an action! However, we'll take one
  71. more optional-but-useful step and give the action a "nice" title in the admin.
  72. By default, this action would appear in the action list as "Make published" --
  73. the function name, with underscores replaced by spaces. That's fine, but we
  74. can provide a better, more human-friendly name by giving the
  75. ``make_published`` function a ``short_description`` attribute::
  76. def make_published(modeladmin, request, queryset):
  77. queryset.update(status='p')
  78. make_published.short_description = "Mark selected stories as published"
  79. .. note::
  80. This might look familiar; the admin's ``list_display`` option uses the
  81. same technique to provide human-readable descriptions for callback
  82. functions registered there, too.
  83. Adding actions to the :class:`ModelAdmin`
  84. -----------------------------------------
  85. Next, we'll need to inform our :class:`ModelAdmin` of the action. This works
  86. just like any other configuration option. So, the complete ``admin.py`` with
  87. the action and its registration would look like::
  88. from django.contrib import admin
  89. from myapp.models import Article
  90. def make_published(modeladmin, request, queryset):
  91. queryset.update(status='p')
  92. make_published.short_description = "Mark selected stories as published"
  93. class ArticleAdmin(admin.ModelAdmin):
  94. list_display = ['title', 'status']
  95. ordering = ['title']
  96. actions = [make_published]
  97. admin.site.register(Article, ArticleAdmin)
  98. That code will give us an admin change list that looks something like this:
  99. .. image:: _images/adding-actions-to-the-modeladmin.png
  100. That's really all there is to it! If you're itching to write your own actions,
  101. you now know enough to get started. The rest of this document just covers more
  102. advanced techniques.
  103. Handling errors in actions
  104. --------------------------
  105. If there are foreseeable error conditions that may occur while running your
  106. action, you should gracefully inform the user of the problem. This means
  107. handling exceptions and using
  108. :meth:`django.contrib.admin.ModelAdmin.message_user` to display a user friendly
  109. description of the problem in the response.
  110. Advanced action techniques
  111. ==========================
  112. There's a couple of extra options and possibilities you can exploit for more
  113. advanced options.
  114. Actions as :class:`ModelAdmin` methods
  115. --------------------------------------
  116. The example above shows the ``make_published`` action defined as a simple
  117. function. That's perfectly fine, but it's not perfect from a code design point
  118. of view: since the action is tightly coupled to the ``Article`` object, it
  119. makes sense to hook the action to the ``ArticleAdmin`` object itself.
  120. That's easy enough to do::
  121. class ArticleAdmin(admin.ModelAdmin):
  122. ...
  123. actions = ['make_published']
  124. def make_published(self, request, queryset):
  125. queryset.update(status='p')
  126. make_published.short_description = "Mark selected stories as published"
  127. Notice first that we've moved ``make_published`` into a method and renamed the
  128. ``modeladmin`` parameter to ``self``, and second that we've now put the string
  129. ``'make_published'`` in ``actions`` instead of a direct function reference. This
  130. tells the :class:`ModelAdmin` to look up the action as a method.
  131. Defining actions as methods gives the action more straightforward, idiomatic
  132. access to the :class:`ModelAdmin` itself, allowing the action to call any of the
  133. methods provided by the admin.
  134. .. _custom-admin-action:
  135. For example, we can use ``self`` to flash a message to the user informing her
  136. that the action was successful::
  137. class ArticleAdmin(admin.ModelAdmin):
  138. ...
  139. def make_published(self, request, queryset):
  140. rows_updated = queryset.update(status='p')
  141. if rows_updated == 1:
  142. message_bit = "1 story was"
  143. else:
  144. message_bit = "%s stories were" % rows_updated
  145. self.message_user(request, "%s successfully marked as published." % message_bit)
  146. This make the action match what the admin itself does after successfully
  147. performing an action:
  148. .. image:: _images/actions-as-modeladmin-methods.png
  149. Actions that provide intermediate pages
  150. ---------------------------------------
  151. By default, after an action is performed the user is simply redirected back
  152. to the original change list page. However, some actions, especially more
  153. complex ones, will need to return intermediate pages. For example, the
  154. built-in delete action asks for confirmation before deleting the selected
  155. objects.
  156. To provide an intermediary page, simply return an
  157. :class:`~django.http.HttpResponse` (or subclass) from your action. For
  158. example, you might write a simple export function that uses Django's
  159. :doc:`serialization functions </topics/serialization>` to dump some selected
  160. objects as JSON::
  161. from django.http import HttpResponse
  162. from django.core import serializers
  163. def export_as_json(modeladmin, request, queryset):
  164. response = HttpResponse(content_type="application/json")
  165. serializers.serialize("json", queryset, stream=response)
  166. return response
  167. Generally, something like the above isn't considered a great idea. Most of the
  168. time, the best practice will be to return an
  169. :class:`~django.http.HttpResponseRedirect` and redirect the user to a view
  170. you've written, passing the list of selected objects in the GET query string.
  171. This allows you to provide complex interaction logic on the intermediary
  172. pages. For example, if you wanted to provide a more complete export function,
  173. you'd want to let the user choose a format, and possibly a list of fields to
  174. include in the export. The best thing to do would be to write a small action
  175. that simply redirects to your custom export view::
  176. from django.contrib import admin
  177. from django.contrib.contenttypes.models import ContentType
  178. from django.http import HttpResponseRedirect
  179. def export_selected_objects(modeladmin, request, queryset):
  180. selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
  181. ct = ContentType.objects.get_for_model(queryset.model)
  182. return HttpResponseRedirect("/export/?ct=%s&ids=%s" % (ct.pk, ",".join(selected)))
  183. As you can see, the action is the simple part; all the complex logic would
  184. belong in your export view. This would need to deal with objects of any type,
  185. hence the business with the ``ContentType``.
  186. Writing this view is left as an exercise to the reader.
  187. .. _adminsite-actions:
  188. Making actions available site-wide
  189. ----------------------------------
  190. .. method:: AdminSite.add_action(action, name=None)
  191. Some actions are best if they're made available to *any* object in the admin
  192. site -- the export action defined above would be a good candidate. You can
  193. make an action globally available using :meth:`AdminSite.add_action()`. For
  194. example::
  195. from django.contrib import admin
  196. admin.site.add_action(export_selected_objects)
  197. This makes the ``export_selected_objects`` action globally available as an
  198. action named "export_selected_objects". You can explicitly give the action
  199. a name -- good if you later want to programmatically :ref:`remove the action
  200. <disabling-admin-actions>` -- by passing a second argument to
  201. :meth:`AdminSite.add_action()`::
  202. admin.site.add_action(export_selected_objects, 'export_selected')
  203. .. _disabling-admin-actions:
  204. Disabling actions
  205. -----------------
  206. Sometimes you need to disable certain actions -- especially those
  207. :ref:`registered site-wide <adminsite-actions>` -- for particular objects.
  208. There's a few ways you can disable actions:
  209. Disabling a site-wide action
  210. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  211. .. method:: AdminSite.disable_action(name)
  212. If you need to disable a :ref:`site-wide action <adminsite-actions>` you can
  213. call :meth:`AdminSite.disable_action()`.
  214. For example, you can use this method to remove the built-in "delete selected
  215. objects" action::
  216. admin.site.disable_action('delete_selected')
  217. Once you've done the above, that action will no longer be available
  218. site-wide.
  219. If, however, you need to re-enable a globally-disabled action for one
  220. particular model, simply list it explicitly in your ``ModelAdmin.actions``
  221. list::
  222. # Globally disable delete selected
  223. admin.site.disable_action('delete_selected')
  224. # This ModelAdmin will not have delete_selected available
  225. class SomeModelAdmin(admin.ModelAdmin):
  226. actions = ['some_other_action']
  227. ...
  228. # This one will
  229. class AnotherModelAdmin(admin.ModelAdmin):
  230. actions = ['delete_selected', 'a_third_action']
  231. ...
  232. Disabling all actions for a particular :class:`ModelAdmin`
  233. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  234. If you want *no* bulk actions available for a given :class:`ModelAdmin`, simply
  235. set :attr:`ModelAdmin.actions` to ``None``::
  236. class MyModelAdmin(admin.ModelAdmin):
  237. actions = None
  238. This tells the :class:`ModelAdmin` to not display or allow any actions,
  239. including any :ref:`site-wide actions <adminsite-actions>`.
  240. Conditionally enabling or disabling actions
  241. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  242. .. method:: ModelAdmin.get_actions(request)
  243. Finally, you can conditionally enable or disable actions on a per-request
  244. (and hence per-user basis) by overriding :meth:`ModelAdmin.get_actions`.
  245. This returns a dictionary of actions allowed. The keys are action names, and
  246. the values are ``(function, name, short_description)`` tuples.
  247. Most of the time you'll use this method to conditionally remove actions from
  248. the list gathered by the superclass. For example, if I only wanted users
  249. whose names begin with 'J' to be able to delete objects in bulk, I could do
  250. the following::
  251. class MyModelAdmin(admin.ModelAdmin):
  252. ...
  253. def get_actions(self, request):
  254. actions = super().get_actions(request)
  255. if request.user.username[0].upper() != 'J':
  256. if 'delete_selected' in actions:
  257. del actions['delete_selected']
  258. return actions