When the Django Admin uses the Select widget, the default/top/no-selection option is "--------":
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...