5 .. 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
12 In these cases, Django's admin lets you write and register "actions" -- simple
13 functions that get called with a list of objects selected on the change list
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:
21 .. image:: _images/user_actions.png
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
30 If you wish to override this behavior, simply write a custom action which
31 accomplishes deletion in your preferred manner -- for example, by calling
32 ``Model.delete()`` for each of the selected items.
34 For more background on bulk deletion, see the documentation on :ref:`object
35 deletion <topics-db-queries-delete>`.
37 Read on to find out how to add your own actions to this list.
42 The easiest way to explain actions is by example, so let's dive in.
44 A common use case for admin actions is the bulk updating of a model. Imagine a
45 simple news application with an ``Article`` model::
47 from django.db import models
55 class Article(models.Model):
56 title = models.CharField(max_length=100)
57 body = models.TextField()
58 status = models.CharField(max_length=1, choices=STATUS_CHOICES)
60 def __unicode__(self):
63 A common task we might perform with a model like this is to update an
64 article's status from "draft" to "published". We could easily do this in the
65 admin one article at a time, but if we wanted to bulk-publish a group of
66 articles, it'd be tedious. So, let's write an action that lets us change an
67 article's status to "published."
69 Writing action functions
70 ------------------------
72 First, we'll need to write a function that gets called when the action is
73 trigged from the admin. Action functions are just regular functions that take
76 * The current :class:`ModelAdmin`
77 * An :class:`~django.http.HttpRequest` representing the current request,
78 * A :class:`~django.db.models.query.QuerySet` containing the set of
79 objects selected by the user.
81 Our publish-these-articles function won't need the :class:`ModelAdmin` or the
82 request object, but we will use the queryset::
84 def make_published(modeladmin, request, queryset):
85 queryset.update(status='p')
89 For the best performance, we're using the queryset's :ref:`update method
90 <topics-db-queries-update>`. Other types of actions might need to deal
91 with each object individually; in these cases we'd just iterate over the
95 do_something_with(obj)
97 That's actually all there is to writing an action! However, we'll take one
98 more optional-but-useful step and give the action a "nice" title in the admin.
99 By default, this action would appear in the action list as "Make published" --
100 the function name, with underscores replaced by spaces. That's fine, but we
101 can provide a better, more human-friendly name by giving the
102 ``make_published`` function a ``short_description`` attribute::
104 def make_published(modeladmin, request, queryset):
105 queryset.update(status='p')
106 make_published.short_description = "Mark selected stories as published"
110 This might look familiar; the admin's ``list_display`` option uses the
111 same technique to provide human-readable descriptions for callback
112 functions registered there, too.
114 Adding actions to the :class:`ModelAdmin`
115 -----------------------------------------
117 Next, we'll need to inform our :class:`ModelAdmin` of the action. This works
118 just like any other configuration option. So, the complete ``admin.py`` with
119 the action and its registration would look like::
121 from django.contrib import admin
122 from myapp.models import Article
124 def make_published(modeladmin, request, queryset):
125 queryset.update(status='p')
126 make_published.short_description = "Mark selected stories as published"
128 class ArticleAdmin(admin.ModelAdmin):
129 list_display = ['title', 'status']
131 actions = [make_published]
133 admin.site.register(Article, ArticleAdmin)
135 That code will give us an admin change list that looks something like this:
137 .. image:: _images/article_actions.png
139 That's really all there is to it! If you're itching to write your own actions,
140 you now know enough to get started. The rest of this document just covers more
143 Advanced action techniques
144 ==========================
146 There's a couple of extra options and possibilities you can exploit for more
149 Actions as :class:`ModelAdmin` methods
150 --------------------------------------
152 The example above shows the ``make_published`` action defined as a simple
153 function. That's perfectly fine, but it's not perfect from a code design point
154 of view: since the action is tightly coupled to the ``Article`` object, it
155 makes sense to hook the action to the ``ArticleAdmin`` object itself.
157 That's easy enough to do::
159 class ArticleAdmin(admin.ModelAdmin):
162 actions = ['make_published']
164 def make_published(self, request, queryset):
165 queryset.update(status='p')
166 make_published.short_description = "Mark selected stories as published"
168 Notice first that we've moved ``make_published`` into a method and renamed the
169 `modeladmin` parameter to `self`, and second that we've now put the string
170 ``'make_published'`` in ``actions`` instead of a direct function reference. This
171 tells the :class:`ModelAdmin` to look up the action as a method.
173 Defining actions as methods gives the action more straightforward, idiomatic
174 access to the :class:`ModelAdmin` itself, allowing the action to call any of the
175 methods provided by the admin.
177 .. _custom-admin-action:
179 For example, we can use ``self`` to flash a message to the user informing her
180 that the action was successful::
182 class ArticleAdmin(admin.ModelAdmin):
185 def make_published(self, request, queryset):
186 rows_updated = queryset.update(status='p')
187 if rows_updated == 1:
188 message_bit = "1 story was"
190 message_bit = "%s stories were" % rows_updated
191 self.message_user(request, "%s successfully marked as published." % message_bit)
193 This make the action match what the admin itself does after successfully
194 performing an action:
196 .. image:: _images/article_actions_message.png
198 Actions that provide intermediate pages
199 ---------------------------------------
201 By default, after an action is performed the user is simply redirected back
202 to the original change list page. However, some actions, especially more
203 complex ones, will need to return intermediate pages. For example, the
204 built-in delete action asks for confirmation before deleting the selected
207 To provide an intermediary page, simply return an
208 :class:`~django.http.HttpResponse` (or subclass) from your action. For
209 example, you might write a simple export function that uses Django's
210 :doc:`serialization functions </topics/serialization>` to dump some selected
213 from django.http import HttpResponse
214 from django.core import serializers
216 def export_as_json(modeladmin, request, queryset):
217 response = HttpResponse(mimetype="text/javascript")
218 serializers.serialize("json", queryset, stream=response)
221 Generally, something like the above isn't considered a great idea. Most of the
222 time, the best practice will be to return an
223 :class:`~django.http.HttpResponseRedirect` and redirect the user to a view
224 you've written, passing the list of selected objects in the GET query string.
225 This allows you to provide complex interaction logic on the intermediary
226 pages. For example, if you wanted to provide a more complete export function,
227 you'd want to let the user choose a format, and possibly a list of fields to
228 include in the export. The best thing to do would be to write a small action
229 that simply redirects to your custom export view::
231 from django.contrib import admin
232 from django.contrib.contenttypes.models import ContentType
233 from django.http import HttpResponseRedirect
235 def export_selected_objects(modeladmin, request, queryset):
236 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
237 ct = ContentType.objects.get_for_model(queryset.model)
238 return HttpResponseRedirect("/export/?ct=%s&ids=%s" % (ct.pk, ",".join(selected)))
240 As you can see, the action is the simple part; all the complex logic would
241 belong in your export view. This would need to deal with objects of any type,
242 hence the business with the ``ContentType``.
244 Writing this view is left as an exercise to the reader.
246 .. _adminsite-actions:
248 Making actions available site-wide
249 ----------------------------------
251 .. method:: AdminSite.add_action(action[, name])
253 Some actions are best if they're made available to *any* object in the admin
254 site -- the export action defined above would be a good candidate. You can
255 make an action globally available using :meth:`AdminSite.add_action()`. For
258 from django.contrib import admin
260 admin.site.add_action(export_selected_objects)
262 This makes the `export_selected_objects` action globally available as an
263 action named `"export_selected_objects"`. You can explicitly give the action
264 a name -- good if you later want to programatically :ref:`remove the action
265 <disabling-admin-actions>` -- by passing a second argument to
266 :meth:`AdminSite.add_action()`::
268 admin.site.add_action(export_selected_objects, 'export_selected')
270 .. _disabling-admin-actions:
275 Sometimes you need to disable certain actions -- especially those
276 :ref:`registered site-wide <adminsite-actions>` -- for particular objects.
277 There's a few ways you can disable actions:
279 Disabling a site-wide action
280 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282 .. method:: AdminSite.disable_action(name)
284 If you need to disable a :ref:`site-wide action <adminsite-actions>` you can
285 call :meth:`AdminSite.disable_action()`.
287 For example, you can use this method to remove the built-in "delete selected
290 admin.site.disable_action('delete_selected')
292 Once you've done the above, that action will no longer be available
295 If, however, you need to re-enable a globally-disabled action for one
296 particular model, simply list it explicitly in your ``ModelAdmin.actions``
299 # Globally disable delete selected
300 admin.site.disable_action('delete_selected')
302 # This ModelAdmin will not have delete_selected available
303 class SomeModelAdmin(admin.ModelAdmin):
304 actions = ['some_other_action']
308 class AnotherModelAdmin(admin.ModelAdmin):
309 actions = ['delete_selected', 'a_third_action']
313 Disabling all actions for a particular :class:`ModelAdmin`
314 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
316 If you want *no* bulk actions available for a given :class:`ModelAdmin`, simply
317 set :attr:`ModelAdmin.actions` to ``None``::
319 class MyModelAdmin(admin.ModelAdmin):
322 This tells the :class:`ModelAdmin` to not display or allow any actions,
323 including any :ref:`site-wide actions <adminsite-actions>`.
325 Conditionally enabling or disabling actions
326 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328 .. method:: ModelAdmin.get_actions(request)
330 Finally, you can conditionally enable or disable actions on a per-request
331 (and hence per-user basis) by overriding :meth:`ModelAdmin.get_actions`.
333 This returns a dictionary of actions allowed. The keys are action names, and
334 the values are ``(function, name, short_description)`` tuples.
336 Most of the time you'll use this method to conditionally remove actions from
337 the list gathered by the superclass. For example, if I only wanted users
338 whose names begin with 'J' to be able to delete objects in bulk, I could do
341 class MyModelAdmin(admin.ModelAdmin):
344 def get_actions(self, request):
345 actions = super(MyModelAdmin, self).get_actions(request)
346 if request.user.username[0].upper() != 'J':
347 if 'delete_selected' in actions:
348 del actions['delete_selected']