0
0
Djangoframework~10 mins

String representation with __str__ in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String representation with __str__
Define model class
Add __str__ method
Create model instance
Call str(instance)
Return string from __str__
Display string in admin or shell
This flow shows how defining __str__ in a Django model returns a readable string when the instance is printed or displayed.
Execution Sample
Django
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)

    def __str__(self):
        return self.title

book = Book(title='Django Basics')
print(str(book))
Defines a Book model with __str__ returning the title, then prints the string representation of a Book instance.
Execution Table
StepActionEvaluationResult
1Define Book model with title fieldClass createdBook class ready
2Add __str__ method returning self.titleMethod added__str__ returns title string
3Create Book instance with title='Django Basics'Instance createdbook.title = 'Django Basics'
4Call str(book)Calls book.__str__()Returns 'Django Basics'
5Print result of str(book)Output to consoleDjango Basics
💡 Execution stops after printing the string representation of the book instance.
Variable Tracker
VariableStartAfter 1After 2After 3Final
book.titleundefinedundefinedundefined'Django Basics''Django Basics'
Key Moments - 2 Insights
Why does print(book) show the title instead of a default object address?
Because the __str__ method is defined to return the title string, print(book) calls __str__ and shows that string (see execution_table step 4).
What happens if __str__ is not defined in the model?
Without __str__, printing the instance shows a default Python object address like <Book object at 0x...>, not a friendly string (not shown in execution_table but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does step 4 do?
ACreates a new Book instance
BPrints the title directly without calling __str__
CCalls the __str__ method of the book instance
DDeletes the book instance
💡 Hint
Check the 'Action' and 'Evaluation' columns in step 4 of the execution_table.
At which step is the book instance's title set to 'Django Basics'?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Result' column in step 3 of the execution_table.
If __str__ returned a fixed string 'Book', what would print(str(book)) output at step 5?
A'Django Basics'
B'Book'
CAn error message
DThe default object address
💡 Hint
Refer to the execution_table step 4 and imagine __str__ returns 'Book' instead of self.title.
Concept Snapshot
Django models can define __str__ to return a friendly string.
This string shows when printing the instance or in admin.
Syntax: def __str__(self): return 'your string'
Use self fields to customize output.
Without __str__, print shows object address.
Full Transcript
In Django, the __str__ method in a model class defines how instances show as strings. When you print a model instance or see it in the admin, Django calls __str__ to get a readable name. For example, if a Book model has a title field, defining __str__ to return self.title means printing the book shows its title. Without __str__, printing shows a default object address which is not user-friendly. This visual trace shows defining __str__, creating an instance, calling str(), and printing the result step-by-step.