actions.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. .. _ref-contrib-admin-actions:
  2. =============
  3. Admin actions
  4. =============
  5. .. versionadded:: 1.1
  6. .. currentmodule:: django.contrib.admin
  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. In these cases, Django's admin lets you write and register "actions" -- simple
  12. functions that get called with a list of objects selected on the change list
  13. page.
  14. If you look at any change list in the admin, you'll see this feature in
  15. action; Django ships with a "delete selected objects" action available to all
  16. models. For example, here's the user module from Django's built-in
  17. :mod:`django.contrib.auth` app:
  18. .. image:: _images/user_actions.png
  19. Read on to find out how to add your own actions to this list.
  20. Writing actions
  21. ===============
  22. The easiest way to explain actions is by example, so let's dive in.
  23. A common use case for admin actions is the bulk updating of a model. Imagine a simple
  24. news application with an ``Article`` model::
  25. from django.db import models
  26. STATUS_CHOICES = (
  27. ('d', 'Draft'),
  28. ('p', 'Published'),
  29. ('w', 'Withdrawn'),
  30. )
  31. class Article(models.Model):
  32. title = models.CharField(max_length=100)
  33. body = models.TextField()
  34. status = models.CharField(max_length=1, choices=STATUS_CHOICES)
  35. def __unicode__(self):
  36. return self.title
  37. A common task we might perform with a model like this is to update an
  38. article's status from "draft" to "published". We could easily do this in the
  39. admin one article at a time, but if we wanted to bulk-publish a group of
  40. articles, it'd be tedious. So, let's write an action that lets us change an
  41. article's status to "published."
  42. Writing action functions
  43. ------------------------
  44. First, we'll need to write a function that gets called when the action is
  45. trigged from the admin. Action functions are just regular functions that take
  46. two arguments: an :class:`~django.http.HttpRequest` representing the current
  47. request, and a :class:`~django.db.models.QuerySet` containing the set of
  48. objects selected by the user. Our publish-these-articles function won't need
  49. the request object, but we will use the queryset::
  50. def make_published(request, queryset):
  51. queryset.update(status='p')
  52. .. note::
  53. For the best performance, we're using the queryset's :ref:`update method
  54. <topics-db-queries-update>`. Other types of actions might need to deal
  55. with each object individually; in these cases we'd just iterate over the
  56. queryset::
  57. for obj in queryset:
  58. do_something_with(obj)
  59. That's actually all there is to writing an action! However, we'll take one
  60. more optional-but-useful step and give the action a "nice" title in the admin.
  61. By default, this action would appear in the action list as "Make published" --
  62. the function name, with underscores replaced by spaces. That's fine, but we
  63. can provide a better, more human-friendly name by giving the
  64. ``make_published`` function a ``short_description`` attribute::
  65. def make_published(request, queryset):
  66. queryset.update(status='p')
  67. make_published.short_description = "Mark selected stories as published"
  68. .. note::
  69. This might look familiar; the admin's ``list_display`` option uses the
  70. same technique to provide human-readable descriptions for callback
  71. functions registered there, too.
  72. Adding actions to the :class:`ModelAdmin`
  73. -----------------------------------------
  74. Next, we'll need to inform our :class:`ModelAdmin` of the action. This works
  75. just like any other configuration option. So, the complete ``admin.py`` with
  76. the action and its registration would look like::
  77. from django.contrib import admin
  78. from myapp.models import Article
  79. def make_published(request, queryset):
  80. queryset.update(status='p')
  81. make_published.short_description = "Mark selected stories as published"
  82. class ArticleAdmin(admin.ModelAdmin):
  83. list_display = ['title', 'status']
  84. ordering = ['title']
  85. actions = [make_published]
  86. admin.site.register(Article, ArticleAdmin)
  87. That code will give us an admin change list that looks something like this:
  88. .. image:: _images/article_actions.png
  89. That's really all there is to it! If you're itching to write your own actions,
  90. you now know enough to get started. The rest of this document just covers more
  91. advanced techniques.
  92. Advanced action techniques
  93. ==========================
  94. There's a couple of extra options and possibilities you can exploit for more
  95. advanced options.
  96. Actions as :class:`ModelAdmin` methods
  97. --------------------------------------
  98. The example above shows the ``make_published`` action defined as a simple
  99. function. That's perfectly fine, but it's not perfect from a code design point
  100. of view: since the action is tightly coupled to the ``Article`` object, it
  101. makes sense to hook the action to the ``ArticleAdmin`` object itself.
  102. That's easy enough to do::
  103. class ArticleAdmin(admin.ModelAdmin):
  104. ...
  105. actions = ['make_published']
  106. def make_published(self, request, queryset):
  107. queryset.update(status='p')
  108. make_published.short_description = "Mark selected stories as published"
  109. Notice first that we've moved ``make_published`` into a method (remembering to
  110. add the ``self`` argument!), and second that we've now put the string
  111. ``'make_published'`` in ``actions`` instead of a direct function reference.
  112. This tells the :class:`ModelAdmin` to look up the action as a method.
  113. Defining actions as methods is especially nice because it gives the action
  114. access to the :class:`ModelAdmin` itself, allowing the action to call any of
  115. the methods provided by the admin.
  116. For example, we can use ``self`` to flash a message to the user informing her
  117. that the action was successful::
  118. class ArticleAdmin(admin.ModelAdmin):
  119. ...
  120. def make_published(self, request, queryset):
  121. rows_updated = queryset.update(status='p')
  122. if rows_updated == 1:
  123. message_bit = "1 story was"
  124. else:
  125. message_bit = "%s stories were" % rows_updated
  126. self.message_user(request, "%s successfully marked as published." % message_bit)
  127. This make the action match what the admin itself does after successfully
  128. performing an action:
  129. .. image:: _images/article_actions_message.png
  130. Actions that provide intermediate pages
  131. ---------------------------------------
  132. By default, after an action is performed the user is simply redirected back
  133. the the original change list page. However, some actions, especially more
  134. complex ones, will need to return intermediate pages. For example, the
  135. built-in delete action asks for confirmation before deleting the selected
  136. objects.
  137. To provide an intermediary page, simply return an
  138. :class:`~django.http.HttpResponse` (or subclass) from your action. For
  139. example, you might write a simple export function that uses Django's
  140. :ref:`serialization functions <topics-serialization>` to dump some selected
  141. objects as JSON::
  142. from django.http import HttpResponse
  143. from django.core import serializers
  144. def export_as_json(request, queryset):
  145. response = HttpResponse(mimetype="text/javascript")
  146. serialize.serialize(queryset, stream=response)
  147. return response
  148. Generally, something like the above isn't considered a great idea. Most of the
  149. time, the best practice will be to return an
  150. :class:`~django.http.HttpResponseRedirect` and redirect the user to a view
  151. you've written, passing the list of selected objects in the GET query string.
  152. This allows you to provide complex interaction logic on the intermediary
  153. pages. For example, if you wanted to provide a more complete export function,
  154. you'd want to let the user choose a format, and possibly a list of fields to
  155. include in the export. The best thing to do would be to write a small action that simply redirects
  156. to your custom export view::
  157. from django.contrib import admin
  158. from django.contrib.contenttypes.models import ContentType
  159. from django.http import HttpResponseRedirect
  160. def export_selected_objects(request, queryset):
  161. selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
  162. ct = ContentType.objects.get_for_model(queryset.model)
  163. return HttpResponseRedirect("/export/?ct=%s&ids=%s" % (ct.pk, ",".join(selected)))
  164. As you can see, the action is the simple part; all the complex logic would
  165. belong in your export view. This would need to deal with objects of any type,
  166. hence the business with the ``ContentType``.
  167. Writing this view is left as an exercise to the reader.
  168. Making actions available globally
  169. ---------------------------------
  170. Some actions are best if they're made available to *any* object in the admin
  171. -- the export action defined above would be a good candidate. You can make an
  172. action globally available using :meth:`AdminSite.add_action()`::
  173. from django.contrib import admin
  174. admin.site.add_action(export_selected_objects)