Extension for the Django admin panel that allows passing additional parameters to actions by creating intermediate pages with forms.
Project description
django-admin-action-forms
Extension for the Django admin panel that allows passing additional parameters to actions by creating intermediate pages with forms.
🚀 Overview
Do you need confirmation pages for your actions in Django admin?
Does creating multiple actions in Django admin that only differ in arguments sound familiar?
Have you ever added a somewhat hacky way to pass additional parameters to an action?
If so, this package is for you!
This is how it looks in action:
By adding a few lines of code, you can create actions with custom forms that will be displayed in an intermediate page before the action is executed. Data from the form will be passed to the action as an additional argument.
Simple and powerful!
🎉 Features
- Requires minimal configuration, easy to use
- Supports all modern Django versions (3.2.x, 4.x.x, 5.x.x)
- Built on top of Django's templates and forms, matches the Django admin style
- Supports
fields
/fieldsets
,filter_horizontal
/filter_vertical
andautocomplete_fields
- Works with custom widgets
🔌 Instalation
- Install using
pip
:
$ pip3 install django-admin-action-forms
- Add
'django_admin_action_forms'
to yourINSTALLED_APPS
setting.
INSTALLED_APPS = [
...
'django_admin_action_forms',
]
-
Include
'django_admin_action_forms.urls'
in yoururls.py
file.Required if you want to use
autocomplete_fields
in your actions' forms.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin-action-forms/", include("django_admin_action_forms.urls")),
path("admin/", admin.site.urls),
...
]
✏️ Example
Below you can see an example of how to create an action with a custom form.
If needed, you can:
- display list of objects that will be affected by using
list_objects
, - add short description with
help_text
, - use custom widgets by specifying them for each field,
- reorder or group fields with
fields
andfieldsets
, - use autocomplete for
Model
s or simple choices by usingautocomplete_fields
.
# admin.py
from django.http import HttpRequest
from django import forms
from django.contrib import admin
from django.db.models import QuerySet
from django_admin_action_forms import action_with_form
from django_admin_action_forms.forms import AdminActionForm
from .models import Order
class GenerateReportActionForm(AdminActionForm):
output_format = forms.ChoiceField(
label="Output format", choices=[(1, ".csv"), (2, ".pdf"), (3, ".json")]
)
from_date = forms.DateField(label="From date", initial="2024-07-15")
to_date = forms.DateField(label="To date", initial="2024-07-22")
max_orders_per_page = forms.IntegerField(
label="Max orders per page",
help_text="Only used when output format is PDF",
required=False,
)
comment = forms.CharField(label="Comment", required=False)
class Meta:
help_text = "This action will generate a sales report for selected orders and download it in the selected format."
fieldsets = [
(
None,
{"fields": ["output_format"]}
),
(
"Date range",
{"fields": ["from_date", "to_date"]}
),
(
"Other",
{"fields": ["max_orders_per_page", "comment"], "classes": ["collapse"]}
),
]
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
@action_with_form(
GenerateReportActionForm,
description="Generate sales report for selected orders",
)
def generate_report(self, request: HttpRequest, queryset: QuerySet, data: dict):
output_format = data["output_format"]
...
actions = [
generate_report,
...
]
📄 Documentation
@action_with_form(form_class, *, permissions=None, description=None)
Decorator that can be used instead of @admin.action
to create action with custom form.
Functions decorated with @action_with_form
should accept additional argument data
that will contain cleaned data from the form.
ActionForm
Base class for creating action forms, it replaces field widgets
Nearly always you will want to subclass AdminActionForm
instead of ActionForm
, as it provides additional features.
AdminActionForm
In addition to ActionForm
, it replaces default text inputs for DateField
, TimeField
, SplitDateTimeField
with respective admin widgets.
Most of the this is a class you want to subclass when creating action forms.
ActionForm.Meta
Additional configuration for action forms. It works similarly to some ModelAdmin
options.
list_objects
Default: False
If True
, the intermediate page will display a list of objects that will be affected by the action similarly
to the intermediate page for built-in delete_selected
action.
help_text
Default: None
Text displayed between the form and the list of objects or form in the intermediate page.
fields
Works similar to
ModelAdmin.fields
.
Specifies the fields that should be displayed in the form. If fieldsets
is provided, fields
will be ignored.
fieldsets
Works similar to
ModelAdmin.fieldsets
If both fields
and fieldsets
are provided, fieldsets
will be used.
filter_horizontal
Default: None
Similar to
ModelAdmin.filter_horizontal
.
Sets fields that should use horizontal filter widget. It should be a list of field names.
filter_vertical
Default: None
Similar to
ModelAdmin.filter_vertical
.
Sets fields that should use vertical filter widget. It should be a list of field names.
autocomplete_fields
In order for autocomplete to you have to include
'django_admin_action_forms.urls'
in yoururls.py
file.
Default: None
Similar to
ModelAdmin.autocomplete_fields
.
Sets fields that should use autocomplete widget. It should be a list of field names.
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
Built Distribution
Hashes for django_admin_action_forms-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c27ec9adf2d6de4a10c99c19150e09eaf923f1e6863d6d1ac4f8e27a1a95d50d |
|
MD5 | 41c7a1d8da1e09aeec8cc6faeaf7e842 |
|
BLAKE2b-256 | ea66a589be2f297f31fd445e3779aac031c5cc607aca7a8ff2c6f1c8876b89dc |
Hashes for django_admin_action_forms-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fab096234b6e2d358ba84cd85a8188ccfe61a99f2707ec1dead31c8dd0a1f3b5 |
|
MD5 | a3b7110be104d86ca3d9344abf0dcd6f |
|
BLAKE2b-256 | 3bc156fe987dd8dedcda669cc24ff6f1a3808441dced5fe6fc7ea9d66d2d2c30 |