问题描述
我想将名为---------( BLANK_CHOICE_DASH
)的默认选定操作更改为另一个特定操作。有没有更好的方法来实现这一点,而不是添加一些JavaScript代码来覆盖加载时间的操作?
I would like to change the default selected action named "---------" (BLANK_CHOICE_DASH
) to another specific action. Is there a better way to implement this than adding some javascript code that would override the action in load time?
推荐答案
1.Override您的 ModelAdmin
中的 get_action_choices()
方法,清除默认空白选项并重新排列列表。
1.Override the get_action_choices()
method in your ModelAdmin
, clear the default blank choice and reorder the list。
class YourModelAdmin(ModelAdmin):
def get_action_choices(self, request):
choices = super(DocumentAdmin, self).get_action_choices(request)
# choices is a list, just change it.
# the first is the BLANK_CHOICE_DASH
choices.pop(0)
# do something to change the list order
# the first one in list will be default option
choices.reverse()
return choices
2.具体action.Override ModelAdmin.changelist_view
,使用 extra_context
更新 action_form
2.Specific action.Override ModelAdmin.changelist_view
, use extra_context
to update action_form
ChoiceField.initial
用于设置默认选择的选项。
所以如果你的动作名称是print_it,你可以这样做。
ChoiceField.initial
used to set the default selected choice.so if your action name is "print_it", you can do this.
class YourModelAdmin(ModelAdmin):
def changelist_view(self,request, **kwargs):
choices = self.get_action_choices(request)
choices.pop(0) # clear default_choices
action_form = self.action_form(auto_id=None)
action_form.fields['action'].choices = choices
action_form.fields['action'].initial = 'print_it'
extra_context = {'action_form': action_form}
return super(DocumentAdmin, self).changelist_view(request, extra_context)
这篇关于设置Django管理员默认动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!