Skip to main content

Parametrized actions for Django admin site

Project description

django-admin-form-action Latest Version

Test Status codecov Python Support

django-admin-form-action is intended to implement parametrized actions on the Django admin site. Action parameters are passed through an intermediate form as it shown below.

demo

The demonstrated functionality can be implemented with django-admin-form-action in the following way:

from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group, User

from admin_form_action import form_action


class GroupsForm(forms.Form):
    groups = forms.ModelMultipleChoiceField(queryset=Group.objects.all())

    def add_user(self, user: User):
        user.groups.add(*self.cleaned_data['groups'])

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    actions = ['add_to_groups']

    @form_action(GroupsForm)
    @admin.action(description='Add selected users to certain groups')
    def add_to_groups(self, request, queryset):
        # Validated form is injected by `@form_action` to `request.form`
        groups_form = request.form
        for user in queryset:
            groups_form.add_user(user)

Install

pip install django-admin-form-action
INSTALLED_APPS = [
    ...
    'admin_form_action',
    ...
]

Development

Run demo app

poetry install
poetry run django-admin migrate --settings=tests.app.settings
poetry run django-admin runserver --settings=tests.app.settings

Test

poetry run pytest

Lint

poetry run isort .
poetry run ruff . 
poetry run mypy .

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_admin_form_action-1.0.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_admin_form_action-1.0.0.tar.gz.

File metadata

  • Download URL: django_admin_form_action-1.0.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Darwin/21.6.0

File hashes

Hashes for django_admin_form_action-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3849d9976daf47aa60f02c5eb273932bfbcdaa1bcfd024a9ecb0b383ad643e3e
MD5 1a235d0e8bac8f5fbdb155dd9bad05e2
BLAKE2b-256 9317052f899c7270e4340c5bc550f611aadec4f92e20febf695f5b16237a0354

See more details on using hashes here.

File details

Details for the file django_admin_form_action-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_form_action-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6df55b83b8ff1f4d2069abdea9823c9475195c411711ff3a51bba9e09e492e70
MD5 b311dcf87a25375c16a318fd4652c75b
BLAKE2b-256 5e0319fd780fdb010918dbcf54086c425a0f1a78c3e456ac9d83ef63a8eb9a0b

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