Easy-to-use actions for Django admin inlines
Install django_inlines_actions
pip install django_inlines_actions
Add
inlines_actions
to yourINSTALLED_APPS
.Add the
InlineActionsModelAdminMixin
to yourModelAdmin
.Add the
InlineActionsMixin
to yourTabularInline
.Add
inline_actions
attribute to yourTabularInline
as a tuple of your actions.inline_actions = ('your_action_name', 'another_action_name',)
Every action must have following signature:
def your_action_name(self, request, obj, parent_obj):
Action is basically a link to another url with corresponding view. It does not depends on form as some other solutions and not execute on Enter hit. Action should return
None
to return to the current changeform or aHttpResponse
instance. If you want do disableActions
column setinline_actions
toNone
.inline_actions = ('change_title',) def change_title(self, request, obj, parent_obj): obj.title = obj.title + ' | ' + parent_obj.name obj.save()
Permissions are instance of list/tuple classes and can be either custom function or django built-in permission.
def your_action_name(self, request, obj, parent_obj): pass your_action_name.permissions = ('permission_func_name', 'django_builtin_permission_name',)permissions function must have following signature:
def permission_func_name(self, request, obj, parent_obj):Must return False to fail permission check otherwise permission will be passed.
If
permissions
returnsFalse
action will no be rendered for user and action will raisePermissionDenied
on execution.
Short Description are either custom function or simple string.
def your_action_name(self, request, obj, parent_obj): pass your_action_name.short_description = 'My Uniq Description'your_action_name.short_description = 'short_description_func_name'short_description function must have following signature:
def short_description_func_name(self, request, obj, parent_obj):
Css Class are either custom function or simple string.
def your_action_name(self, request, obj, parent_obj): pass your_action_name.css_class = 'my-css-class-name'your_action_name.css_class = 'css_class_func_name'css_class function must have following signature:
def css_class_func_name(self, request, obj, parent_obj):
from django.contrib import admin
from inlines_actions import InlineActionsModelAdminMixin, InlineActionsMixin
from .models import Article1, Author1
class ArticleInline(
InlineActionsMixin,
admin.TabularInline,
):
model = Article1
inline_actions = ('make_published',)
def make_published(self, request, obj, parent_obj):
obj.is_published = not obj.is_published
obj.save()
def make_published_permission(self, request, obj, parent_obj):
if not request.user.is_superuser:
return False
def make_published_short_description(self, request, obj, parent_obj):
return 'Published Make'
def make_published_css_class(self, request, obj, parent_obj):
return 'some_class'
make_published.permissions = ('make_published_permission', 'example_app:can_change_article')
make_published.short_description = make_published_short_description
make_published.css_class = make_published_css_class
class AuthorAdmin(
InlineActionsModelAdminMixin,
admin.ModelAdmin,
):
inlines = (ArticleInline,)
admin.site.register(Author1, AuthorAdmin)
pip install requirements.txt tox