For example (not the case, but just to illustrate) if I wanted to add an action to set a specific field in the selected items to X. Is it possible to add an action to allow X to be entered as opposed to hard coded?
Asked
Active
Viewed 1.2k times
2 Answers
21
See: Actions that Provide Intermediate Pages
You can't do it right from the changelist page, but on your intermediate page, you could have a form that would allow the user to enter a value, and then use that value for the action.

Chris Pratt
- 232,153
- 36
- 385
- 444
13
It is possible with Django 1.5, though its a little hackish. I am not sure with what other older Django versions it's possible.
You write your own ModelAdmin subclass. ModelAdmin has an attribute called action_form
which determines the form shown before Go
button on changelist page. You can write your own form, and set it as action_form on your ModelAdmin subclass.
from django import forms
from django.contrib.admin.helpers import ActionForm
# ActionForm is the default form used by Django
# You can extend this class
class XForm(ActionForm):
x_field = forms.CharField()
class YourModelAdmin(admin.ModelAdmin):
action_form = XForm
With these changes, you will have a CharField
, where you can put value for X
.
And use x_field
in your action function.
def set_x_on_objects(modeladmin, request, queryset):
for obj in queryset:
obj.something = request.POST['x_field']
# do whatever else you want
class YourModelAdmin(admin.ModelAdmin):
action_form = XForm
actions = [set_x_on_objects]

nik_m
- 11,825
- 4
- 43
- 57

Akshar Raaj
- 14,231
- 7
- 51
- 45
-
to use this – should be usedNils Zenker May 17 '18 at 10:03
-
Works for Django 2.2. Thanks :) – nik_m Sep 14 '21 at 07:28
-
Should be marked as the answer. Thank you for sharing. – jlandercy May 20 '22 at 13:28
-
This works in Django 4.0.1, but breaks delete function – Mike Sep 19 '22 at 22:40
-
The action form applies to all actions, not good. – Fabio Caccamo Jan 25 '23 at 14:15