Table of contents
1.
Introduction
2.
__str__() method
3.
Object Display Name in Django Models
4.
Change Object Display Name using __str__ function
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Change Object Display Name using __str__ function

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Django is one of Python's most popular, trusted, and reliable web development frameworks. With Django, we can quickly create web applications without worrying about installation or dependency problems that we usually find with other frameworks.

While creating a model in Django, we might notice that almost instinctively, everyone adds a few extra lines that include the __str__ method or maybe not, but let's learn about the __str__ method anyways.

The __str__ method lets us convert an object into the string representation. Generally, we use this method while creating models in Django, but we can also use it in other places. This article will learn to make changes to our Django models, to change object display name using __str__ function in the model.

__str__() method

The __str__() method lets us convert an object into the string representation. This method is called whenever we call str() on any object. Django uses the str() method in many places but, most importantly, displays an object in the Django admin panel as the value inserted in the template. It is always good to return the Django model's friendly, human-readable string representation for the __str__() method. 

Object Display Name in Django Models

For this article, Let's consider a project named "CodingNinjas" with an app called "Coding Ninjas Studio".

We will create a Django model in the models.py file by writing the following code,

from django.db import models


# Make the model here.
class Coding Ninjas StudioModel(models.Model):
    example_field = models.CharField(max_length = 180)
You can also try this code with Online Python Compiler
Run Code

 

We created the 'Coding Ninjas StudioModel' model with 'example_field' in the code. Now, let's run makemigrations and migrate command to create the database and then we will render the above Django model. 

Let's create an instance of the model using the string "CodingNinjas is best".

Django Admin Panel

If we try to see the same object in the admin interface, we will find that the object has the name of 'Coding Ninjas StudioModel object(1)'.

Django Admin Panel

Change Object Display Name using __str__ function

In the above section, we saw that the object display name for the instance we created was auto-generated as 'Coding Ninjas StudioModel Object(1)'. We will try to change object display name using __str__ function. 

We can use the def __str__(self) function to change the display name in our Django model. The __str__() function in the Django model returns a string that is the same as the display name of the instances of the model. So, let's try to change our model.py file,

from django.db import models


# Make the model here.
class Coding Ninjas StudioModel(models.Model):
    example_field = models.CharField(max_length = 180)
 
    def __str__(self):
         return "New Name"
You can also try this code with Online Python Compiler
Run Code

 

The above code will permanently change the display name of the objects to 'New Name'. 

Django Admin Panel

Note:- We can also set the display name of the object dynamically; for example, we can set the name of the object of Coding Ninjas StudioModel as self.example_field,

from django.db import models


# Make the model here.
class Coding Ninjas StudioModel(models.Model):
    example_field = models.CharField(max_length = 180)
 
    def __str__(self):
         return self.example_field
You can also try this code with Online Python Compiler
Run Code

 

The above code will make the object display name as the value of example_field.

Django Admin Panel

Frequently Asked Questions

  1. What Is __str__ function?
    The __str__() function in the Django model returns a string that is the same as the display name of the instances of the model. 
     
  2. What is the difference between __str__ and __repr__?
    Just like the __str__ function __repr__ function also returns the representation of an object in string format.
    The __str__ function is there to return a human-readable format, which is helpful for logging or displaying any information about the object. While, the __repr__ function is there to return the "official" string representation of the object, which can help construct the object again. It mainly serves the purpose of debugging and development.
     
  3. Why is __str__ called a dunder function?
    Dunder functions (dunder is short for "double under") start with a double underscore and end with a double underscore. Some common examples of dunder function are __str__, __repr__, __init__.
     
  4. What are the benefits to change object display name using __str__ function?
    We change object display name using __str__ function because of the following reasons:
    1. String representation makes it easier to identify the objects.
    2. Proper string representation makes it easier to find errors in logs.

Key Takeaways

This article taught us how to change object display name using __str__ function in Django Model. We built a Django model and named it differently using the __str__ function. 

Don't stop here. Check out the blogs Best Django Books, Top 30 Basic Django Interview Questions: Part 1, and Top 30 Intermediate Django Interview Questions: Part 2.

We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.

Live masterclass