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?
Asked
Active
Viewed 1,511 times
3

d33tah
- 10,999
- 13
- 68
- 158
-
Possible duplicate of [default value for django choice field](http://stackoverflow.com/questions/22257622/default-value-for-django-choice-field) – solarissmoke Nov 12 '15 at 07:04
-
@solarissmoke: that's not the same. I want it in the *admin actions*. – d33tah Nov 12 '15 at 07:06
-
the same concept should apply - you just need to override the corresponding admin forms? – solarissmoke Nov 12 '15 at 07:12
3 Answers
2
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(YourModelAdmin, 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.Specific action.Override ModelAdmin.changelist_view
, use extra_context
to update action_form
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)

xavierskip
- 431
- 4
- 12
-
choices = super(DocumentAdmin, self).get_action_choices(request) should be
choices = super(MyModelAdmin, self).get_action_choices(request)
– Nils Zenker May 16 '18 at 13:05 -
0
I think you can override the get_action_choices()
method in the ModelAdmin
.
class MyModelAdmin(admin.ModelAdmin):
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices

Rod Xavier
- 3,983
- 1
- 29
- 41
0
in your admin.py file
class MyModelAdmin(admin.ModelAdmin):
def get_action_choices(self, request, **kwargs):
choices = super(MyModelAdmin, 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
and in your class
class TestCaseAdmin(MyModelAdmin):

Nils Zenker
- 659
- 7
- 11