The Wayback Machine - https://web.archive.org/web/20240717024323/https://www.geeksforgeeks.org/change-object-display-name-using-__str__-function-django-models-python/
Open In App

Change Object Display Name using __str__ function – Django Models | Python

Last Updated : 13 Feb, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 geeks.

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.




from django.db import models
from django.db.models import Model
# Create your models here.
  
class GeeksModel(Model):
    geeks_field = models.CharField(max_length = 200)


After running makemigrations and migrate on Django and rendering above model, let us try to create an instance using string “GfG is Best“.
django-object-name-display
Now it will display the same object created as GeeksModel object(1) in the admin interface.
display name django models

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




from django.db import models
from django.db.models import Model
# Create your models here.
  
class GeeksModel(Model):
    geeks_field = models.CharField(max_length = 200)
     
    def __str__(self):
         return "something"


This will display the objects as something always in the admin interface.
django-display-name-something

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.
django-display-name-gfg-is-best.

Note that str function always returns a string and one can modify that according to one’s convenience.

Also Check –



Previous Article
Next Article

Similar Reads

Creating multiple user types and using proxy models in Python Django
In this article, we will understand the concept of the proxy model and the implementation of multiple user types depending on the requirements of your application in Django. What is the Proxy model in Django? An inherited proxy model can have a few extra functionalities, methods, and different behaviors than the parent model as defined by the creat
9 min read
Python | Relational fields in Django models
Prerequisite: Django models Django models represent real-world entities, and it is rarely the case that real-world entities are entirely independent of each other. Hence Django supports relational databases and allows us to establish relations between different models. There are three types of relational fields which Django supports: many-to-one, m
4 min read
Django Models | Set - 1
Prerequisites : Django Creating apps Models - According to Django Models, A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models
2 min read
Django Models | Set - 2
Model Fields - Model fields define the datatype which will be stored in the provided variable. To store price, the integer type is more suitable. To store height, length etc. float/decimal type is more suitable. To store title or heading, it is better to provide character limit. For writing an article, it is better to have a textbox with no charact
2 min read
BigIntegerField - Django Models
BigIntegerField is a 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput. Syntax field_name = models.BigIntegerField(**options) Django Model BigIntegerField Explanation Illustration of BigAutoField using an Examp
4 min read
AutoField - Django Models
According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won’t need to use this directly because a primary key field will automatically be added to your model if you don’t specify otherwise. By default, Django gives each model the following field: id = models.AutoField(primary
4 min read
BigAutoField - Django Models
BigAutoField is a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807. One can create a BigAutoField using the following syntax, id = models.BigAutoField(primary_key=True, **options) This is an auto-incrementing primary key just like AutoField. Django Model BigAutoField Explanation Illust
4 min read
BooleanField - Django Models
BooleanField is a true/false field. It is like a bool field in C/C++. The default form widget for this field is CheckboxInput, or NullBooleanSelect if null=True. The default value of BooleanField is None when Field.default isn’t defined. One can define the default value as true or false by setting the default attribute to true/false simultaneously.
4 min read
CharField - Django Models
CharField is a string field, for small- to large-sized strings. It is like a string field in C/C++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput. CharField has one extra required argument: CharField.max_length The ma
4 min read
DateField - Django Models
DateField is a field that stores date, represented in Python by a datetime.date instance. As the name suggests, this field is used to store an object of date created in python. The default form widget for this field is a TextInput. The admin can add a JavaScript calendar and a shortcut for “Today” explicitly. Syntax: field_name = models.DateField(*
5 min read
Practice Tags :
three90RightbarBannerImg