71

As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164

8 Answers8

106

the verbose name of the field is the (optional) first parameter at field construction.

Javier
  • 60,510
  • 8
  • 78
  • 126
  • 4
    wondering: is this possible via admin only, when for example unregistering and reregistering a model? answer is yes, either with a custom form (drawbacks for more complex fields) or via a custom function property of the admin. looking for a more minimal solution, if possible... – benzkji Sep 29 '21 at 11:35
41

If your field is a property (a method) then you should use short_description:

class Person(models.Model):
    ...

    def address_report(self, instance):
        ...
    # short_description functions like a model field's verbose_name
    address_report.short_description = "Address"
Seperman
  • 4,254
  • 1
  • 28
  • 27
33

As Javier suggested you can use verbose name in your fields in model.py. Example as below,

class Employee(models.Model):
     name = models.CharField(max_length = 100)
     dob = models.DateField('Date Of Birth')
     doj = models.DateField(verbose_name='Date Of Joining')
     mobile=models.IntegerField(max_length = 12)
     email = models.EmailField(max_length=50)
     bill = models.BooleanField(db_index=True,default=False)
     proj = models.ForeignKey(Project, verbose_name='Project')

Here the dob,doj and proj files will display its name in admin form as per the verbose_name mentioned to those fields.

maciek
  • 3,198
  • 2
  • 26
  • 33
Naggappan Ramukannan
  • 2,564
  • 9
  • 36
  • 59
9
from django.db import models

class MyClassName(models.Model):    
    field_name = models.IntegerField(verbose_name='Field Caption')
Majid Zandi
  • 4,371
  • 5
  • 21
  • 29
9

If you want change the field label only on particular admin model without changing field of the model:

class MyModelAdmin(admin.ModelAdmin):    
    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        form.base_fields["name"].label = "New label"
        return form
Evgeni Shudzel
  • 231
  • 2
  • 5
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are less useful to a community of learners. Please [edit] your answer to include an explanation of how and why the code solves the problem, when it should be used, what its limitations are, and if possible a link to relevant documentation. – ljmc Dec 29 '22 at 15:46
  • 1
    Might be a `not considered good answers` but it does the trick! – JoeManFoo May 19 '23 at 08:14
5

Building on Javier's answer; if you need one label in forms (on the front-end) and another label on admin it is best to set internal (admin) one in the model and overwrite it on forms. Admin will of course use the label in the model field automatically.

muhuk
  • 15,777
  • 9
  • 59
  • 98
2

You can use verbose_name to change a field label in Django Admin as shown below.

# "models.py"

class MyModel(models.Model):                # Here
    name = models.CharField(max_length=255, verbose_name="My name")

In addition, you can put a field label to the 1st argument without verbose_name as shown below:

# "models.py"

class MyModel(models.Model):
    name = models.CharField("My name", max_length=255)
                            # Here
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

As far as the changelist view is concerned, this can be done with a simple field function.

class MyModelAdmin(admin.ModelAdmin): 

    list_display = (
        '_name',
        ...,
    )

    @admin.display(description='Customized field name here')
    def _name(self, obj):
        return obj.name
ytyng
  • 575
  • 6
  • 8