Metadata-Version: 2.1
Name: django-admin-form-action
Version: 0.1.0
Summary: Parametrized actions for Django admin site
Home-page: https://github.com/pauk-slon/django-admin-extended-actions
License: MIT
Author: Dmitry Kolyagin
Author-email: dmitry.kolyagin@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: Framework :: Django :: 3
Classifier: Framework :: Django :: 4
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: django (>=3)
Project-URL: Repository, https://github.com/pauk-slon/django-admin-extended-actions
Description-Content-Type: text/markdown

# django-admin-form-action

[![Test Status][test-status-image]][test-status-link]
[![codecov][codecov-image]][codecov-link]

`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](demo.gif "Demo")

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

```python
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

```shell
pip install django-admin-form-action
```

```python
INSTALLED_APPS = [
    ...
    'admin_form_action',
    ...
]
```

## Run demo app

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

[codecov-image]: https://codecov.io/gh/pauk-slon/django-admin-form-action/graph/badge.svg?token=QCY3CW2ZVG
[codecov-link]: https://codecov.io/gh/pauk-slon/django-admin-form-action
[test-status-image]: https://github.com/pauk-slon/django-admin-form-action/actions/workflows/test.yaml/badge.svg
[test-status-link]: https://github.com/pauk-slon/django-admin-form-action/actions/workflows/test.yaml

