Object actions
Object actions are ModelAdmin methods that run against one object. Register
them with @object_action and list their names in ObjectActionsOption.
from django.contrib import messages
from django.http import HttpRequest, HttpResponseRedirect
from django.utils.translation import gettext_lazy as _
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
class BookAdmin(ChassisAdminMixin, ModelAdmin):
options = [ObjectActionsOption(actions=['publish'])]
@object_action(
description=_('Publish'), permission='change', confirmation=_('Publish this book?'), color=ButtonColor.SUCCESS
)
def publish(self, request: HttpRequest, obj: Book) -> HttpResponseRedirect | None:
obj.publish()
messages.success(request, _('Published.'))
return None
Decorator
@object_action(
description,
*,
permission=None,
confirmation=None,
color=ButtonColor.PRIMARY,
condition=None,
condition_method=None,
)
| Parameter | Purpose |
|---|---|
description | Button label (lazy strings allowed) |
permission | view / add / change / delete or a custom codename |
confirmation | If set, Chassis shows a confirm form before calling the method |
color | ButtonColor |
condition | callable(admin, request, obj) -> bool |
condition_method | Name 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', 'archive'])
Only listed names are exposed. An undecorated method in the list fails configuration.
Confirmation
When confirmation is set, Chassis renders
admin/chassis/confirmed_action.html with ConfirmedActionForm. The method
runs only after POST confirm.