Skip to main content

Object actions

Added in 1.0.1

Object actions are ModelAdmin methods that run against one object. Register them with @object_action and list their names in ObjectActionsOption.

Changed in 1.0.7

Actions can include an icon from the Font Awesome bundle shipped by Chassis.

Added in 1.0.15

Set opens_modal=True to load the action response through the shared Chassis modal client. The action keeps the same compact colored button as every other object action, and its href remains available as an accessible fallback.

from django.contrib.admin import ModelAdmin
from django.core.exceptions import PermissionDenied
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from django_chassis.components import AdminModal
from django_chassis.decorators import object_action
from django_chassis.enums import ButtonColor
from django_chassis.mixins import ChassisAdminMixin
from django_chassis.options import ObjectActionsOption

from catalog.models import Book


class BookAdmin(ChassisAdminMixin, ModelAdmin):
options = [ObjectActionsOption(actions=['publish'])]

@object_action(
description=_('Publish'),
permission='change',
color=ButtonColor.SUCCESS,
icon_class='fa-solid fa-bullhorn',
opens_modal=True
)
def publish(self, request: HttpRequest, obj: Book) -> HttpResponse:
if request.method == 'GET':
return TemplateResponse(request, 'admin/chassis/users/security_confirmation.html', {
'chassis_modal': {'title': _('Publish')},
'confirmation_object': str(obj),
'confirmation_message': _('Publish this book?')
})
if request.method != 'POST':
return HttpResponseNotAllowed(['GET', 'POST'])
if request.POST.get('confirm') != 'yes':
raise PermissionDenied
obj.publish()
return AdminModal.redirect(reverse(f'{self.admin_site.name}:catalog_book_change', args=[obj.pk]))

Decorator​

@object_action(
description,
*,
permission=None,
confirmation=None,
color=ButtonColor.PRIMARY,
icon_class=None,
opens_modal=False,
condition=None,
condition_method=None,
)
ParameterPurpose
descriptionButton label (lazy strings allowed)
permissionview / add / change / delete or a custom codename
confirmationText for a browser confirmation on the standard link
colorButtonColor
icon_classOptional Font Awesome CSS classes rendered before the label
opens_modalLoad the action response in the shared Chassis modal
conditioncallable(admin, request, obj) -> bool
condition_methodName of a @classmethod invoked as (request, obj)

condition and condition_method are mutually exclusive.

The method signature is (self, request, obj). Return HttpResponse to redirect or render, or None to stay on the object page.

Route (ModelAdmin):

{admin}/{app}/{model}/{object_id}/actions/{action_name}/

URL name: {app}_{model}_object_action. Permission and condition are checked again on that request, not only when the button is rendered. AdminObjectPage uses the same decorator on a non-model card — see Custom pages.

ObjectActionsOption​

ObjectActionsOption(
actions=['publish'],
dropdown=ObjectActionsDropdownOption(actions=['archive'])
)

Only listed names are exposed. An undecorated method in the list fails configuration.

Added in 1.0.7

dropdown explicitly places its action names under the primary Additional actions menu. Names must be unique across actions and dropdown.actions; an empty dropdown is invalid. Independently of explicit grouping, Chassis automatically renders six or more visible toolbar actions as one dropdown, while five or fewer remain individual buttons.

Confirmation​

In 1.0.23 the dispatcher checks permissions and conditions and then calls the method on GET or POST. The decorator’s confirmation argument supplies browser prompt text. A state-changing method must render the confirmation on GET and perform the write only on a confirmed POST. The modal client opens opens_modal=True actions through GET, so return a Chassis modal fragment from that branch.

Object-action templates that extend admin/object_actions/base_action_view.html now use the shared Chassis page shell and place their content in the object_action_content block. The shell provides compact form, information-table, warning, and action-button styling. Action pages use the full content width. Their lower action panel is aligned to the right and includes the shared Back button; validation errors are summarized in the common danger alert, and required confirmation checkboxes are rendered after the other fields.

See also​