2009-03-23 20:22:56 +00:00
|
|
|
=============
|
|
|
|
Admin actions
|
|
|
|
=============
|
|
|
|
|
|
|
|
.. currentmodule:: django.contrib.admin
|
|
|
|
|
|
|
|
The 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 you
|
|
|
|
need to make the same change to many objects at once, this workflow can be
|
|
|
|
quite tedious.
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
In these cases, Django's admin lets you write and register "actions" --
|
2009-03-23 20:22:56 +00:00
|
|
|
functions that get called with a list of objects selected on the change list
|
|
|
|
page.
|
|
|
|
|
|
|
|
If you look at any change list in the admin, you'll see this feature in
|
|
|
|
action; Django ships with a "delete selected objects" action available to all
|
|
|
|
models. For example, here's the user module from Django's built-in
|
|
|
|
:mod:`django.contrib.auth` app:
|
|
|
|
|
2015-09-11 03:07:10 +00:00
|
|
|
.. image:: _images/admin-actions.png
|
2009-05-14 02:35:16 +00:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
The "delete selected objects" action uses :meth:`QuerySet.delete()
|
2011-09-30 10:28:39 +00:00
|
|
|
<django.db.models.query.QuerySet.delete>` for efficiency reasons, which
|
|
|
|
has an important caveat: your model's ``delete()`` method will not be
|
|
|
|
called.
|
|
|
|
|
2017-09-04 09:24:34 +00:00
|
|
|
If you wish to override this behavior, you can override
|
|
|
|
:meth:`.ModelAdmin.delete_queryset` or write a custom action which does
|
|
|
|
deletion in your preferred manner -- for example, by calling
|
2009-05-14 02:35:16 +00:00
|
|
|
``Model.delete()`` for each of the selected items.
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-05-14 02:35:16 +00:00
|
|
|
For more background on bulk deletion, see the documentation on :ref:`object
|
|
|
|
deletion <topics-db-queries-delete>`.
|
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
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.
|
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
A common use case for admin actions is the bulk updating of a model. Imagine a
|
2019-06-17 14:54:55 +00:00
|
|
|
news application with an ``Article`` model::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
2019-05-15 12:31:42 +00:00
|
|
|
STATUS_CHOICES = [
|
2009-03-23 20:22:56 +00:00
|
|
|
("d", "Draft"),
|
|
|
|
("p", "Published"),
|
|
|
|
("w", "Withdrawn"),
|
2019-05-15 12:31:42 +00:00
|
|
|
]
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
body = models.TextField()
|
|
|
|
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2009-03-23 20:22:56 +00:00
|
|
|
return self.title
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
A common task we might perform with a model like this is to update an
|
|
|
|
article's status from "draft" to "published". We could easily do this in the
|
|
|
|
admin one article at a time, but if we wanted to bulk-publish a group of
|
|
|
|
articles, it'd be tedious. So, let's write an action that lets us change an
|
|
|
|
article's status to "published."
|
|
|
|
|
|
|
|
Writing action functions
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
First, we'll need to write a function that gets called when the action is
|
2019-06-17 14:54:55 +00:00
|
|
|
triggered from the admin. Action functions are regular functions that take
|
2011-09-30 10:28:39 +00:00
|
|
|
three arguments:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* The current :class:`ModelAdmin`
|
|
|
|
* An :class:`~django.http.HttpRequest` representing the current request,
|
|
|
|
* A :class:`~django.db.models.query.QuerySet` containing the set of
|
|
|
|
objects selected by the user.
|
2009-04-06 20:23:33 +00:00
|
|
|
|
|
|
|
Our publish-these-articles function won't need the :class:`ModelAdmin` or the
|
|
|
|
request object, but we will use the queryset::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
def make_published(modeladmin, request, queryset):
|
2009-03-23 20:22:56 +00:00
|
|
|
queryset.update(status="p")
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
.. 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 deal
|
2019-06-17 14:54:55 +00:00
|
|
|
with each object individually; in these cases we'd iterate over the
|
2009-03-23 20:22:56 +00:00
|
|
|
queryset::
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
for obj in queryset:
|
|
|
|
do_something_with(obj)
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
That's actually all there is to writing an action! However, we'll take one
|
|
|
|
more 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 we
|
2021-01-13 16:19:22 +00:00
|
|
|
can provide a better, more human-friendly name by using the
|
|
|
|
:func:`~django.contrib.admin.action` decorator on the ``make_published``
|
|
|
|
function::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
|
|
|
|
...
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
@admin.action(description="Mark selected stories as published")
|
2009-04-06 20:23:33 +00:00
|
|
|
def make_published(modeladmin, request, queryset):
|
2009-03-23 20:22:56 +00:00
|
|
|
queryset.update(status="p")
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
.. note::
|
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
This might look familiar; the admin's
|
|
|
|
:attr:`~django.contrib.admin.ModelAdmin.list_display` option uses a similar
|
|
|
|
technique with the :func:`~django.contrib.admin.display` decorator to
|
|
|
|
provide human-readable descriptions for callback functions registered
|
|
|
|
there, too.
|
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
Adding actions to the :class:`ModelAdmin`
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Next, we'll need to inform our :class:`ModelAdmin` of the action. This works
|
|
|
|
just like any other configuration option. So, the complete ``admin.py`` with
|
|
|
|
the action and its registration would look like::
|
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
@admin.action(description="Mark selected stories as published")
|
2009-04-06 20:23:33 +00:00
|
|
|
def make_published(modeladmin, request, queryset):
|
2009-03-23 20:22:56 +00:00
|
|
|
queryset.update(status="p")
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["title", "status"]
|
|
|
|
ordering = ["title"]
|
|
|
|
actions = [make_published]
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
admin.site.register(Article, ArticleAdmin)
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
That code will give us an admin change list that looks something like this:
|
|
|
|
|
2015-09-11 03:07:10 +00:00
|
|
|
.. image:: _images/adding-actions-to-the-modeladmin.png
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
That's really all there is to it! If you're itching to write your own actions,
|
2019-06-17 14:54:55 +00:00
|
|
|
you now know enough to get started. The rest of this document covers more
|
2009-03-23 20:22:56 +00:00
|
|
|
advanced techniques.
|
|
|
|
|
2012-11-17 21:53:31 +00:00
|
|
|
Handling errors in actions
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
If there are foreseeable error conditions that may occur while running your
|
|
|
|
action, you should gracefully inform the user of the problem. This means
|
2014-04-22 23:05:14 +00:00
|
|
|
handling exceptions and using
|
2012-11-17 21:53:31 +00:00
|
|
|
:meth:`django.contrib.admin.ModelAdmin.message_user` to display a user friendly
|
|
|
|
description of the problem in the response.
|
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
Advanced action techniques
|
|
|
|
==========================
|
|
|
|
|
|
|
|
There's a couple of extra options and possibilities you can exploit for more
|
|
|
|
advanced options.
|
|
|
|
|
|
|
|
Actions as :class:`ModelAdmin` methods
|
|
|
|
--------------------------------------
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
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 sense
|
|
|
|
to hook the action to the ``ArticleAdmin`` object itself.
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
You can do it like this::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
|
...
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
actions = ["make_published"]
|
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
@admin.action(description="Mark selected stories as published")
|
2009-03-23 20:22:56 +00:00
|
|
|
def make_published(self, request, queryset):
|
|
|
|
queryset.update(status="p")
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
Notice first that we've moved ``make_published`` into a method and renamed the
|
2013-03-22 09:50:45 +00:00
|
|
|
``modeladmin`` parameter to ``self``, and second that we've now put the string
|
2009-04-06 20:23:33 +00:00
|
|
|
``'make_published'`` in ``actions`` instead of a direct function reference. This
|
|
|
|
tells the :class:`ModelAdmin` to look up the action as a method.
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
Defining actions as methods gives the action more idiomatic access to the
|
|
|
|
:class:`ModelAdmin` itself, allowing the action to call any of the methods
|
|
|
|
provided by the admin.
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2010-12-11 13:51:14 +00:00
|
|
|
.. _custom-admin-action:
|
|
|
|
|
2020-11-13 21:26:30 +00:00
|
|
|
For example, we can use ``self`` to flash a message to the user informing them
|
2009-03-23 20:22:56 +00:00
|
|
|
that the action was successful::
|
|
|
|
|
2020-04-16 09:26:33 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.utils.translation import ngettext
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
|
...
|
|
|
|
|
|
|
|
def make_published(self, request, queryset):
|
2020-04-16 09:26:33 +00:00
|
|
|
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,
|
|
|
|
)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
This make the action match what the admin itself does after successfully
|
|
|
|
performing an action:
|
|
|
|
|
2015-09-11 03:07:10 +00:00
|
|
|
.. image:: _images/actions-as-modeladmin-methods.png
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-03-23 20:22:56 +00:00
|
|
|
Actions that provide intermediate pages
|
|
|
|
---------------------------------------
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
By default, after an action is performed the user is redirected back to the
|
|
|
|
original change list page. However, some actions, especially more complex ones,
|
|
|
|
will need to return intermediate pages. For example, the built-in delete action
|
|
|
|
asks for confirmation before deleting the selected objects.
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
To provide an intermediary page, return an :class:`~django.http.HttpResponse`
|
2020-11-30 09:07:28 +00:00
|
|
|
(or subclass) from your action. For example, you might write an export function
|
2019-06-17 14:54:55 +00:00
|
|
|
that uses Django's :doc:`serialization functions </topics/serialization>` to
|
|
|
|
dump some selected objects as JSON::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
from django.core import serializers
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.http import HttpResponse
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-04-06 20:28:55 +00:00
|
|
|
def export_as_json(modeladmin, request, queryset):
|
2013-01-31 12:39:29 +00:00
|
|
|
response = HttpResponse(content_type="application/json")
|
2009-05-17 17:13:33 +00:00
|
|
|
serializers.serialize("json", queryset, stream=response)
|
2009-03-23 20:22:56 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
Generally, something like the above isn't considered a great idea. Most of the
|
|
|
|
time, the best practice will be to return an
|
|
|
|
:class:`~django.http.HttpResponseRedirect` and redirect the user to a view
|
|
|
|
you've written, passing the list of selected objects in the GET query string.
|
|
|
|
This allows you to provide complex interaction logic on the intermediary
|
|
|
|
pages. 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 to
|
2009-04-06 20:23:33 +00:00
|
|
|
include in the export. The best thing to do would be to write a small action
|
2019-06-17 14:54:55 +00:00
|
|
|
that redirects to your custom export view::
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.http import HttpResponseRedirect
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-04-06 20:28:55 +00:00
|
|
|
def export_selected_objects(modeladmin, request, queryset):
|
2019-11-04 11:38:31 +00:00
|
|
|
selected = queryset.values_list("pk", flat=True)
|
2009-03-23 20:22:56 +00:00
|
|
|
ct = ContentType.objects.get_for_model(queryset.model)
|
2019-11-04 11:38:31 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
"/export/?ct=%s&ids=%s"
|
|
|
|
% (
|
|
|
|
ct.pk,
|
|
|
|
",".join(str(pk) for pk in selected),
|
|
|
|
)
|
2023-02-28 19:53:28 +00:00
|
|
|
)
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
As you can see, the action is rather short; all the complex logic would belong
|
|
|
|
in your export view. This would need to deal with objects of any type, hence
|
|
|
|
the business with the ``ContentType``.
|
2009-03-23 20:22:56 +00:00
|
|
|
|
|
|
|
Writing this view is left as an exercise to the reader.
|
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
.. _adminsite-actions:
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
Making actions available site-wide
|
|
|
|
----------------------------------
|
2009-03-23 20:22:56 +00:00
|
|
|
|
2015-07-27 12:35:21 +00:00
|
|
|
.. method:: AdminSite.add_action(action, name=None)
|
2009-04-06 20:23:33 +00:00
|
|
|
|
|
|
|
Some actions are best if they're made available to *any* object in the admin
|
|
|
|
site -- the export action defined above would be a good candidate. You can
|
|
|
|
make an action globally available using :meth:`AdminSite.add_action()`. For
|
|
|
|
example::
|
|
|
|
|
|
|
|
from django.contrib import admin
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
admin.site.add_action(export_selected_objects)
|
|
|
|
|
2013-08-15 11:14:10 +00:00
|
|
|
This makes the ``export_selected_objects`` action globally available as an
|
|
|
|
action named "export_selected_objects". You can explicitly give the action
|
2013-07-28 01:45:25 +00:00
|
|
|
a name -- good if you later want to programmatically :ref:`remove the action
|
2009-04-06 20:23:33 +00:00
|
|
|
<disabling-admin-actions>` -- by passing a second argument to
|
|
|
|
:meth:`AdminSite.add_action()`::
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
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 can
|
|
|
|
call :meth:`AdminSite.disable_action()`.
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
For example, you can use this method to remove the built-in "delete selected
|
|
|
|
objects" action::
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
admin.site.disable_action("delete_selected")
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
Once you've done the above, that action will no longer be available
|
|
|
|
site-wide.
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2022-03-11 08:11:42 +00:00
|
|
|
If, however, you need to reenable a globally-disabled action for one
|
2019-06-17 14:54:55 +00:00
|
|
|
particular model, list it explicitly in your ``ModelAdmin.actions`` list::
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
# Globally disable delete selected
|
|
|
|
admin.site.disable_action("delete_selected")
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
# This ModelAdmin will not have delete_selected available
|
|
|
|
class SomeModelAdmin(admin.ModelAdmin):
|
|
|
|
actions = ["some_other_action"]
|
|
|
|
...
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
# This one will
|
|
|
|
class AnotherModelAdmin(admin.ModelAdmin):
|
|
|
|
actions = ["delete_selected", "a_third_action"]
|
|
|
|
...
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
|
|
|
|
Disabling all actions for a particular :class:`ModelAdmin`
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
If you want *no* bulk actions available for a given :class:`ModelAdmin`, set
|
|
|
|
:attr:`ModelAdmin.actions` to ``None``::
|
2009-04-06 20:23:33 +00:00
|
|
|
|
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
|
actions = None
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
This 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)
|
|
|
|
|
2011-09-30 10:28:39 +00:00
|
|
|
Finally, you can conditionally enable or disable actions on a per-request
|
2009-04-06 20:23:33 +00:00
|
|
|
(and hence per-user basis) by overriding :meth:`ModelAdmin.get_actions`.
|
|
|
|
|
|
|
|
This returns a dictionary of actions allowed. The keys are action names, and
|
|
|
|
the values are ``(function, name, short_description)`` tuples.
|
|
|
|
|
2018-11-09 23:52:30 +00:00
|
|
|
For example, if you only want users whose names begin with 'J' to be able
|
|
|
|
to delete objects in bulk::
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
|
...
|
2011-09-30 10:28:39 +00:00
|
|
|
|
2009-04-06 20:23:33 +00:00
|
|
|
def get_actions(self, request):
|
2017-01-22 06:57:14 +00:00
|
|
|
actions = super().get_actions(request)
|
2009-04-06 20:23:33 +00:00
|
|
|
if request.user.username[0].upper() != "J":
|
2011-07-29 09:40:43 +00:00
|
|
|
if "delete_selected" in actions:
|
|
|
|
del actions["delete_selected"]
|
2009-04-06 20:23:33 +00:00
|
|
|
return actions
|
2018-06-18 19:07:29 +00:00
|
|
|
|
|
|
|
.. _admin-action-permissions:
|
|
|
|
|
|
|
|
Setting permissions for actions
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
Actions may limit their availability to users with specific permissions by
|
2021-01-13 16:19:22 +00:00
|
|
|
wrapping the action function with the :func:`~django.contrib.admin.action`
|
|
|
|
decorator and passing the ``permissions`` argument::
|
2018-06-18 19:07:29 +00:00
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
@admin.action(permissions=["change"])
|
2018-06-18 19:07:29 +00:00
|
|
|
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.
|
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
If ``permissions`` has more than one permission, the action will be available
|
|
|
|
as long as the user passes at least one of the checks.
|
2018-06-18 19:07:29 +00:00
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
Available values for ``permissions`` and the corresponding method checks are:
|
2018-06-18 19:07:29 +00:00
|
|
|
|
|
|
|
- ``'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 admin
|
|
|
|
from django.contrib.auth import get_permission_codename
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2018-06-18 19:07:29 +00:00
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
|
actions = ["make_published"]
|
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
@admin.action(permissions=["publish"])
|
2018-06-18 19:07:29 +00:00
|
|
|
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.opts
|
|
|
|
codename = get_permission_codename("publish", opts)
|
|
|
|
return request.user.has_perm("%s.%s" % (opts.app_label, codename))
|
2021-01-13 16:19:22 +00:00
|
|
|
|
|
|
|
The ``action`` decorator
|
|
|
|
========================
|
|
|
|
|
|
|
|
.. function:: action(*, permissions=None, description=None)
|
|
|
|
|
|
|
|
This decorator can be used for setting specific attributes on custom action
|
|
|
|
functions 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, longer
|
|
|
|
names) on the function directly::
|
|
|
|
|
|
|
|
def make_published(self, request, queryset):
|
|
|
|
queryset.update(status="p")
|
2023-02-28 19:53:28 +00:00
|
|
|
|
|
|
|
|
2021-01-13 16:19:22 +00:00
|
|
|
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 it
|
|
|
|
can be useful to use it without arguments as a marker in your source to
|
|
|
|
identify the purpose of the function::
|
|
|
|
|
|
|
|
@admin.action
|
|
|
|
def make_inactive(self, request, queryset):
|
|
|
|
queryset.update(is_active=False)
|
|
|
|
|
|
|
|
In this case it will add no attributes to the function.
|
2023-03-09 12:43:17 +00:00
|
|
|
|
|
|
|
Action descriptions are %-formatted and may contain ``'%(verbose_name)s'``
|
|
|
|
and ``'%(verbose_name_plural)s'`` placeholders, which are replaced,
|
|
|
|
respectively, by the model's :attr:`~django.db.models.Options.verbose_name`
|
|
|
|
and :attr:`~django.db.models.Options.verbose_name_plural`.
|