Skip to main content

django-inline-actions adds actions to the InlineModelAdmin.

Project description

https://img.shields.io/pypi/v/django-inline-actions.svg Build Status Coverage https://img.shields.io/pypi/pyversions/django-inline-actions.svg https://img.shields.io/pypi/status/django-inline-actions.svg https://img.shields.io/pypi/l/django-inline-actions.svg

django-inline-actions adds actions to the InlineModelAdmin.

Screenshot

https://raw.githubusercontent.com/escaped/django-inline-actions/master/example.png

Installation

  1. Install django-inline-actions

    pip install django-inline-actions
  2. Add inline_actions to your INSTALLED_APPS.

Integration

Add the InlineActionMixin to your InlineModelAdmin and the InlineActionsModelAdminMixin to your ModelAdmin. Each action is implemented as a method on the InlineModelAdmin and has the following signature

def action_name(self, request, obj, inline_obj)
  1. request - current request

  2. obj - instance of the parent model

  3. inline_obj - instance on which the action was triggered

and should return None to return to the current changeform or a HttpResponse. Finally, add your method name to the actions property. To add your actions dynamically, you can use the method get_actions(self, request, obj=None) instead.

This module is bundled with two actions for viewing (inline_actions.actions.ViewAction) and deleting (inline_actions.actions.DeleteAction). Just add these classes to your InlineModelAdmin and you’re done.

Example

Imagine a simple news application with the following admin.py.

from django.contrib import admin
from inline_actions.admin import InlineActionsMixin
from inline_actions.admin import InlineActionsModelAdminMixin

from .models import Article, Author


class ArticleInline(InlineActionsMixin,
                    admin.TabularInline):
    model = Article
    actions = []

    def has_add_permission(self):
        return False


@admin.register(Author)
class AuthorAdmin(InlineActionsModelAdminMixin,
                  admin.ModelAdmin):
    inlines = [ArticleInline]
    list_display = ('name',)


@admin.register(Article)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('title', 'status', 'author')

We now want to add two simple actions (view, unpublish) to each article within the AuthorAdmin. The view action redirects to the changeform of the selected instance

from django.core.urlresolvers import reverse
from django.shortcuts import redirect


class ArticleInline(InlineActionsMixin,
                    admin.TabularInline):
    # ...
    actions = ['view']
    # ...

    def view(self, request, obj, inline_obj):
        url = reverse(
            'admin:{}_{}_change'.format(
                inline_obj._meta.app_label,
                inline_obj._meta.model_name,
            ),
            args=(inline_obj.pk,)
        )
        return redirect(url)
    view.short_description = _("View")

Since unpublish depends on article.status we must use get_actions to add this action dynamically.

from django.contrib import admin, messages
from django.utils.translation import ugettext_lazy as _


class ArticleInline(InlineActionsMixin,
                    admin.TabularInline):
    # ...
    def get_actions(self, request, obj=None):
        actions = super(ArticleInline, self).get_actions(request, obj)
        if obj:
            if obj.status == Article.PUBLISHED:
                actions.append('unpublish')
        return actions

    def unpublish(self, request, obj, inline_obj):
        inline_obj.status = Article.DRAFT
        inline_obj.save()
        messages.info(request, _("Article unpublished"))
    unpublish.short_description = _("Unpublish")

Example Application

You can see django-inline-actions in action using the bundled test application test_proj. I recommend to use a virtualenv.

git clone https://github.com/escaped/django-inline-actions.git
cd django-inline-actions/
pip install Django
pip install -e .
cd test_proj
./manage.py migrate
./manage.py createsuperuser
./manage.py runserver

Open http://localhost:8000/admin/ in your browser and create an author and some articles.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-inline-actions-0.1.1.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

django_inline_actions-0.1.1-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-inline-actions-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-inline-actions-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3374374c2bc3b962c9ab1d86ddace9a02ef7c556c3e374e0c32e59935e7342f7
MD5 3760d421577795273786447ab9101f16
BLAKE2b-256 56df12f92c7c946af4892e182e32d029d6b582b6feaeaead00df3f78d8cc989b

See more details on using hashes here.

File details

Details for the file django_inline_actions-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_inline_actions-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e379d83679268f5f243573cba72d4005112cb474b88cfa5b95fe686e880aaea5
MD5 4cbfb7d86590d76d3dac0b152294667b
BLAKE2b-256 ee7cf80d49872254f33be4b13ca0ed702356e0a04f31a651f62a1e066dc529b5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page