0

I have no idea how to remove "--------" default action value in django admin.

or replace "------" with any other string ("Select Option").

enter image description here

Thanks..

Sayse
  • 42,633
  • 14
  • 77
  • 146
SuReSh
  • 1,503
  • 1
  • 22
  • 47

4 Answers4

3

You could try overriding the ModelAdmin.get_action_choices method. However, this is an undocumented, internal method, so I wouldn't recommend changing it unless it's absolutely essential that you remove/replace the dashes.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I want to replace dashes with other string. – SuReSh Feb 19 '16 at 10:58
  • 1
    The method Alasdair linked to has an option for `default_choices` so just override it with a call to super with a different choice for that. – Sayse Feb 19 '16 at 11:01
2

remove default action

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

replace with other string

class YourModelAdmin(ModelAdmin):
    def get_action_choices(self, request):
    default_choices = [("", "-----other string----")]
    return super(DocumentAdmin, self).get_action_choices(request, default_choices)

set default action for your action, you can see my answer here https://stackoverflow.com/a/41276533/1265727

Community
  • 1
  • 1
xavierskip
  • 431
  • 4
  • 12
0

With the help of @Alasdair my issue is resolved.

I use this code in model.py and its change my default option value to "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
SuReSh
  • 1,503
  • 1
  • 22
  • 47
  • You shouldn't have to import `BLANK_CHOICE_DASH` or duplicate the code from the method. Just define `MY_BLANK_CHOICE_DASH = [("", "My text")]`, then do `return super(MyModelAdmin, self).get_action_choices(request, default_choices=MY_BLANK_CHOICE_DASH)`. – Alasdair Feb 29 '16 at 13:53
0

After some time later, i also find a solution of this problem..

You can also override empty_value_display for all admin pages with AdminSite.empty_value_display, or for specific fields like this:

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 = '???'
SuReSh
  • 1,503
  • 1
  • 22
  • 47