答案 0 :(得分:3)
您可以尝试覆盖ModelAdmin.get_action_choices
方法。但是,这是一种无证的内部方法,因此我不建议更改它,除非您删除/替换破折号绝对必要。
答案 1 :(得分:1)
删除默认操作
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)
return choices
替换为其他字符串
class YourModelAdmin(ModelAdmin):
def get_action_choices(self, request):
default_choices = [("", "-----other string----")]
return super(DocumentAdmin, self).get_action_choices(request, default_choices)
为您的操作设置默认操作,您可以在此处查看我的答案https://stackoverflow.com/a/41276533/1265727
答案 2 :(得分:0)
在@Alasdair的帮助下,我的问题得到了解决。
我在model.py中使用此代码,并将其默认选项值更改为"Select Options"
from django.db.models.fields import BLANK_CHOICE_DASH
BLANK_CHOICE_DASH = [("", "---------")]
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
答案 3 :(得分:0)
过了一段时间后,我也找到了解决这个问题的方法..
您还可以使用AdminSite.empty_value_display覆盖所有管理页面的empty_value_display,或者覆盖以下特定字段:
from django.contrib import admin
class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title', 'view_birth_date')
def view_birth_date(self, obj):
return obj.birth_date
view_birth_date.short_name = 'birth_date'
view_birth_date.empty_value_display = '???'