{"id":3968,"library":"django-object-actions","title":"django-object-actions","description":"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.","status":"active","version":"5.0.0","language":"en","source_language":"en","source_url":"https://github.com/crccheck/django-object-actions","tags":["django","admin","actions","object-tools"],"install":[{"cmd":"pip install django-object-actions","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core functionality as a Django app, requires Django to run.","package":"django","optional":false}],"imports":[{"symbol":"ObjectActions","correct":"from object_actions import ObjectActions"},{"note":"Introduced in v4.1.0 as an alternative and more feature-rich way to define actions compared to the 'objectactions' list.","symbol":"object_action","correct":"from object_actions import object_action"}],"quickstart":{"code":"import os\nfrom django.contrib import admin\nfrom django.db import models\nfrom django.http import HttpResponseRedirect\nfrom object_actions import ObjectActions, object_action\n\n# Define a simple model for demonstration\nclass MyModel(models.Model):\n    name = models.CharField(max_length=100)\n    value = models.IntegerField(default=0)\n\n    def __str__(self):\n        return self.name\n\n    class Meta:\n        app_label = 'myapp' # Ensure app_label is set for isolated example\n\n# Configure admin with object actions\nclass MyModelAdmin(ObjectActions, admin.ModelAdmin):\n    list_display = ('name', 'value')\n    objectactions = ['reset_value_legacy'] # Old way to register actions\n\n    def reset_value_legacy(self, request, obj):\n        obj.value = 0\n        obj.save()\n        self.message_user(request, f\"Value for {obj.name} reset to 0.\")\n        return HttpResponseRedirect(request.get_full_path())\n\n    @object_action(label='Increment Value', description='Increments the object\\'s value by 1.')\n    def increment_value_decorator(self, request, obj):\n        obj.value += 1\n        obj.save()\n        self.message_user(request, f\"Value for {obj.name} incremented to {obj.value}.\")\n        return HttpResponseRedirect(request.get_full_path())\n\n# In a real Django project, you would register this in myapp/admin.py\n# For this standalone example, we just define it.\n\n# Example usage (conceptual, for actual setup you need a Django project):\n# 1. Add 'object_actions' to INSTALLED_APPS in settings.py\n# 2. Add 'myapp' to INSTALLED_APPS (if MyModel is in myapp)\n# 3. In myapp/admin.py:\n#    admin.site.register(MyModel, MyModelAdmin)\n\nprint(\"To use, add 'object_actions' to INSTALLED_APPS and register your ModelAdmin inheriting from ObjectActions.\")\nprint(\"Define methods on your ModelAdmin and list them in 'objectactions' or use the @object_action decorator.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or newer before installing django-object-actions v5.0.0+.","message":"Older Python versions are no longer supported. v2.0.0 dropped Python 2, v3.0.0 dropped Python 3.4, and v4.0.0 dropped Python 3.6. The current version (v5.0.0) requires Python >= 3.9.","severity":"breaking","affected_versions":"<5.0.0"},{"fix":"Ensure your action method signatures conform to `def my_action(self, request, obj):` or `def my_action(self, request, obj, form):`.","message":"Object action methods must accept at least `request` and `obj` as arguments. An optional third argument `form` can be added if your action needs to interact with the model's admin form.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For custom permission handling, override `has_change_permission` on your ModelAdmin, or for decorator-based actions, use the `permission_required` argument on `@object_action`.","message":"By default, object actions require the user to have 'change' permissions for the model. If a user lacks this permission, the action button will not be visible. Custom permission checks can be implemented.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always return an `HttpResponseRedirect(request.get_full_path())` to refresh the page after an action, or another appropriate `HttpResponse`.","message":"It's crucial for object action methods to return an `HttpResponse` (e.g., `HttpResponseRedirect`) or `None`. If `None` is returned, the user will remain on the same page. Not returning a valid HTTP response can lead to unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new actions, prefer using the `@object_action` decorator. For existing actions, consider refactoring to use the decorator for better maintainability and clearer metadata.","message":"While listing action names in the `objectactions` attribute still works, the `@object_action` decorator (introduced in v4.1.0) is the recommended modern approach as it allows specifying a `label`, `description`, `attrs`, and `permission_required` directly on the method.","severity":"gotcha","affected_versions":"<4.1.0 (for missing decorator features)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}