django-object-actions

5.0.0 · active · verified Sat Apr 11

django-object-actions is a Django app that provides an easy way to add custom object-level actions to your Django admin interface. Instead of model-wide actions, these are specific to individual objects, appearing on the change page. The current version is 5.0.0, with minor releases occurring every few months and major breaking changes typically yearly, often tied to Django or Python version support updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to add object-level actions to a Django ModelAdmin. It shows both the legacy method of listing action names in the `objectactions` attribute and the more modern `@object_action` decorator (introduced in v4.1.0) which allows for richer metadata like labels and descriptions. Actions appear on the change page of individual model objects in the Django admin.

import os
from django.contrib import admin
from django.db import models
from django.http import HttpResponseRedirect
from object_actions import ObjectActions, object_action

# Define a simple model for demonstration
class MyModel(models.Model):
    name = models.CharField(max_length=100)
    value = models.IntegerField(default=0)

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'myapp' # Ensure app_label is set for isolated example

# Configure admin with object actions
class MyModelAdmin(ObjectActions, admin.ModelAdmin):
    list_display = ('name', 'value')
    objectactions = ['reset_value_legacy'] # Old way to register actions

    def reset_value_legacy(self, request, obj):
        obj.value = 0
        obj.save()
        self.message_user(request, f"Value for {obj.name} reset to 0.")
        return HttpResponseRedirect(request.get_full_path())

    @object_action(label='Increment Value', description='Increments the object\'s value by 1.')
    def increment_value_decorator(self, request, obj):
        obj.value += 1
        obj.save()
        self.message_user(request, f"Value for {obj.name} incremented to {obj.value}.")
        return HttpResponseRedirect(request.get_full_path())

# In a real Django project, you would register this in myapp/admin.py
# For this standalone example, we just define it.

# Example usage (conceptual, for actual setup you need a Django project):
# 1. Add 'object_actions' to INSTALLED_APPS in settings.py
# 2. Add 'myapp' to INSTALLED_APPS (if MyModel is in myapp)
# 3. In myapp/admin.py:
#    admin.site.register(MyModel, MyModelAdmin)

print("To use, add 'object_actions' to INSTALLED_APPS and register your ModelAdmin inheriting from ObjectActions.")
print("Define methods on your ModelAdmin and list them in 'objectactions' or use the @object_action decorator.")

view raw JSON →