109👍
the verbose name of the field is the (optional) first parameter at field construction.
43👍
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"
- [Django]-How do I include image files in Django templates?
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-How does django handle multiple memcached servers?
34👍
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.
- [Django]-Django – makemigrations – No changes detected
- [Django]-Django-Admin: CharField as TextArea
- [Django]-How to call function that takes an argument in a Django template?
12👍
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
- [Django]-Django FileField upload is not working for me
- [Django]-Difference between auto_now and auto_now_add
- [Django]-How to filter empty or NULL names in a QuerySet?
10👍
from django.db import models
class MyClassName(models.Model):
field_name = models.IntegerField(verbose_name='Field Caption')
- [Django]-Laravel's dd() equivalent in django
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
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.
- [Django]-How to get POST request values in Django?
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-Django – how to unit test a post request using request.FILES
3👍
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
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
- [Django]-How do I make many-to-many field optional in Django?
- [Django]-How to set up custom middleware in Django?
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
- [Django]-Overriding the save method in Django ModelForm
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)