1

When the Django Admin uses the Select widget, the default/top/no-selection option is "--------":

enter image description here

Is there a way to customize that option? I tried manually modifying the "choices" value of the Select field:

def __init__(self, *args, **kwargs):
    super(MyAdminForm, self).__init__(*args, **kwargs)
    self.fields['my_field'].choices[0] = (u'', 'something else')

but that doesn't work, as the admin still displays "--------"...


Update:
The answers in the suggested duplicate question almost solve my problem... however, the answers there seem to be techniques for giving ALL of the Select fields the same no-selection option.

In my case, the ModelForm in question is used by a TabularInline ModelAdmin, so the form is repeated 20 times on the page, and different instances need a different no-selection value (that is dependent on self.instance).

Also, the most up-voted answer doesn't work with the latest version of Django - the empty_label field now needs to be set on the self.base_fields['my_field'] object before calling super(). But I need self.instance to exist in order to do my customizing, and self.instance is None before super() is called...

Community
  • 1
  • 1
Troy
  • 21,172
  • 20
  • 74
  • 103
  • 1
    possible duplicate of [Customize/remove Django select box blank option](http://stackoverflow.com/questions/739260/customize-remove-django-select-box-blank-option) – MrAlexBailey Aug 31 '15 at 22:27
  • @Jkdc - not quite a duplicate... I don't think... See my update, above. – Troy Aug 31 '15 at 22:59

0 Answers0