Change field name in django admin
Solution 1
What I was looking for is :
name = models.CharField(max_length=200, verbose_name="Nom")
Thanks anyway for your help !
Solution 2
Setting verbose_name
on the model field, as in the accepted answer, does work, but it has some disadvantages:
-
It will also affect the field label in non-admin forms, as mentioned in the comment
-
It changes the definition of the model field, so it results in a new migration
If you only want to change the display name of the field on the admin site, you could either override the entire form, as suggested in this other answer, or simply modify the form field label by extending ModelAdmin.get_form()
, as in the example below:
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, change=False, **kwargs):
form = super().get_form(request, obj, change, **kwargs)
form.base_fields['my_model_field_name'].label = 'new name'
return form
Note that this does not work if the field is in readonly_fields
(at least in Django 2.2.16), because the field will be excluded form.base_fields
(see source). In that case, one option would be to remove the field from readonly_fields
and disable it instead, by adding another line inside get_form()
:
form.base_fields['my_model_field_name'].disabled = True
Solution 3
Look at Field.label. See this: https://docs.djangoproject.com/en/dev/ref/forms/fields/#label
Basically, it's
class MyForm(forms.Form):
name = forms.CharField(label='My Name')
But that requires extending the admin, which may be more than you want to do.
HydrUra
Updated on July 26, 2022Comments
-
HydrUra over 1 year
I am customizing django admin and I would like to change the display name of my fields. I think the answer is here but I can't find it. I already change the table name thanks to Meta class. I have also ordered fields, gathered them, collapsed them...
-
HydrUra over 10 yearsI have already founded this answer but where can I find those forms used to build the admin UI ? Basically, I don't need to call those for admin UI... Somewhere in /usr/local/lib/python3.3/dist-packages/django/contrib/admin I assume...
-
Rob L over 10 yearsYou should install the django debug-toolbar. It will show you all the templates rendered and extended for any view.
-
HydrUra over 10 yearsI have trouble installing debug-toolbar but when it will I think it's gonna be usefull !
-
Tom Carrick almost 10 yearsWorth noting that this will also change the label in all your non-admin forms (and anywhere else you use the verbose_name).