How to Change Display Name of an Object in Django admin interface? Whenever an instance of model is created in Django, it displays the object as ModelName Object(1). This article will explore how to make changes to your Django model using
def __str__(self) to change the display name in the model.
Object Display Name in Django Models
Consider a project named
geeksforgeeks having an app named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into
models.py file of geeks app.
After running makemigrations and migrate on Django and rendering above model, let us try to create an instance using string “GfG is Best“.
Now it will display the same object created as GeeksModel object(1) in the admin interface.
How to change display name of Model Instances in Django admin interface?
To change the display name we will use
def __str__() function in a model. str function in a django model returns a string that is exactly rendered as the display name of instances for that model.
For example, if we change above models.py to
This will display the objects as something always in the admin interface.
Most of the time we name the display name which one could understand using
self object. For example
return self.geeks_field in above function will return the string stored in geeks_field of that object and it will be displayed in admin interface.
Note that str function always returns a string and one can modify that according to one’s convenience.
Also Check –
- Python | Relational fields in Django models
- Django Models
- Django Models | Set - 2
- Django Models | Set - 1
- BinaryField - Django Models
- EmailField - Django Models
- DurationField - Django Models
- IntegerField - Django Models
- FloatField - Django Models
- SmallIntegerField - Django Models
- DateField - Django Models
- DateTimeField - Django Models
- BooleanField - Django Models
- CharField - Django Models
- DecimalField - Django Models
- TimeField - Django Models
- TextField - Django Models
- FileField - Django Models
- FilePathField - Django Models
- ImageField - Django Models
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : NaveenArora