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?
例如(不是这种情况,只是为了说明),如果我想添加一个动作来将所选项目中的特定字段设置为X.是否可以添加一个动作以允许输入X而不是硬编码?
2 个解决方案
#1
12
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.
您无法从更改列表页面直接执行此操作,但在中间页面上,您可以使用允许用户输入值的表单,然后将该值用于操作。
#2
5
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.
有可能使用Django 1.5,虽然它有点hackish。我不确定其他更旧的Django版本是否可行。
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.
您编写自己的ModelAdmin子类。 ModelAdmin有一个名为action_form的属性,它确定在更改列表页面上的Go按钮之前显示的表单。您可以编写自己的表单,并在ModelAdmin子类上将其设置为action_form。
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
.
通过这些更改,您将拥有一个CharField,您可以在其中为X赋值。
And use x_field
in your action function.
并在动作函数中使用x_field。
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]
#1
12
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.
您无法从更改列表页面直接执行此操作,但在中间页面上,您可以使用允许用户输入值的表单,然后将该值用于操作。
#2
5
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.
有可能使用Django 1.5,虽然它有点hackish。我不确定其他更旧的Django版本是否可行。
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.
您编写自己的ModelAdmin子类。 ModelAdmin有一个名为action_form的属性,它确定在更改列表页面上的Go按钮之前显示的表单。您可以编写自己的表单,并在ModelAdmin子类上将其设置为action_form。
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
.
通过这些更改,您将拥有一个CharField,您可以在其中为X赋值。
And use x_field
in your action function.
并在动作函数中使用x_field。
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]