Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Custom Field Validations in Django Models
Next article icon

Customize Object Names with __str__ Method

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When you create instances of a Django model, by default, they appear in the Django admin interface and elsewhere as "ModelName object (1)" (or a similar format). This can make it hard to identify records, especially when you have many objects.

Why Customize Object Display Names?

By default, Django doesn’t know how to represent your objects as readable strings, so it uses a generic placeholder. Customizing the display name helps you and your users quickly identify objects in the admin panel and anywhere else the object is displayed as a string.

This article explains how to customize the display name of your Django model instances by overriding the __str__ method in your model class. This makes the admin interface and other representations much more meaningful and easier to understand.

Example: Default Behavior

Consider a project named geeksforgeeks having an app named geeks

Refer to the following Articles to check how to create Django App and Project:

  • How to Create a Basic Project using MVT in Django?
  • How to Create an App in Django ?

Enter the following code into models.py file of geeks app.

Python
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 migrations and creating an instance with "GfG is Best" as the geeks_field value, the object will appear as GeeksModel object(1) in the admin interface, which isn’t very descriptive.:

display name django models

How to Customize the Display Name Using __str__

To change this, add a __str__ method to your model. This method should return a string that represents the object in a human-readable way.

Here’s how to modify the GeeksModel:

Python
from django.db import models  class GeeksModel(models.Model):     geeks_field = models.CharField(max_length=200)          def __str__(self):         return self.geeks_field  # Returns the value of geeks_field as the display name 

Now, when you create an object with geeks_field="GfG is Best", the admin interface will display "GfG is Best"

instead of the generic "GeeksModel object (1)":

django-object-name-display

Key Points to Remember

  • The __str__ method must return a string.
  • It’s best to return a value that uniquely and clearly identifies the object.
  • You can customize the return string as needed, for example, combining multiple fields:

Next Article
Custom Field Validations in Django Models

N

NaveenArora
Improve
Article Tags :
  • Python
  • Python Django
  • Django-models
Practice Tags :
  • python

Similar Reads

    Django Models
    A Django model is a Python class that represents a database table. Models make it easy to define and work with database tables using simple Python code. Instead of writing complex SQL queries, we use Django’s built-in ORM (Object Relational Mapper), which allows us to interact with the database in a
    8 min read
    Django ORM - Inserting, Updating & Deleting Data
    Django's Object-Relational Mapping (ORM) is one of the key features that simplifies interaction with the database. It allows developers to define their database schema in Python classes and manage data without writing raw SQL queries. The Django ORM bridges the gap between Python objects and databas
    4 min read
    Django Basic App Model - Makemigrations and Migrate
    Django's Object-Relational Mapping (ORM) simplifies database interactions by mapping Python objects to database tables. One of the key features of Django's ORM is migrations, which allow you to manage changes to the database schema.What are Migrations in Django?Migrations are files that store instru
    4 min read
    Add the slug field inside Django Model
    The slug field within Django models is a pivotal step for improving the structure and readability of URLs in web applications. This addition allows developers to automatically generate URL-friendly slugs based on titles, enhancing user experience and search engine optimization (SEO). By implementing
    4 min read
    Intermediate fields in Django - Python
    Prerequisite: Django models, Relational fields in DjangoIn Django, a many-to-many relationship is used when instances of one model can be associated with multiple instances of another model and vice versa. For example, in a shop management system:A Customer can purchase multiple Items.An Item can be
    2 min read
    Uploading images in Django - Python
    Prerequisite - Introduction to DjangoUploading and managing image files is a common feature in many web applications, such as user profile pictures, product images, or photo galleries. In Django, you can handle image uploads easily using the ImageField in models.In this article, we’ll walk through a
    3 min read
    Customize Object Names with __str__ Method
    When you create instances of a Django model, by default, they appear in the Django admin interface and elsewhere as "ModelName object (1)" (or a similar format). This can make it hard to identify records, especially when you have many objects.Why Customize Object Display Names?By default, Django doe
    2 min read
    Custom Field Validations in Django Models
    Field validation ensures that the data entered into a model field meets specific rules before it’s saved to the database. While Django provides built-in validations for common checks, custom field validation lets you enforce your own rules, such as verifying formats, length limits, or complex condit
    3 min read
    Meta Class in Models - Django
    Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. D
    3 min read
    How to use Django Field Choices ?
    Django’s choices option lets you limit a model field to a fixed set of values. It helps keep your data clean and consistent, and automatically shows a dropdown menu in forms and the admin instead of a text box.Choices are defined as pairs: the first value is saved to the database, and the second is
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences