0
0
Djangoframework~20 mins

String representation with __str__ in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Representation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of printing a Django model instance with __str__ defined?
Consider this Django model:
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.title} by {self.author}"

What will print(book) output if book = Book(title='1984', author='Orwell')?
Django
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.title} by {self.author}"

book = Book(title='1984', author='Orwell')
print(book)
ABook(title='1984', author='Orwell')
B<Book object>
C1984 by Orwell
DError: __str__ method missing
Attempts:
2 left
💡 Hint
The __str__ method defines what print(instance) shows.
📝 Syntax
intermediate
2:00remaining
Which __str__ method is correctly defined in a Django model?
Select the option that correctly defines the __str__ method in a Django model to return the model's name attribute.
Django
class Person(models.Model):
    name = models.CharField(max_length=50)

    # Which __str__ method below is correct?
A
def __str__(self):
    print(self.name)
B
def __str__(self):
    return self.name
C
def __str__(self):
    return name
D
def __str__(self):
    return self.name()
Attempts:
2 left
💡 Hint
The __str__ method must return a string, not print it or call attributes incorrectly.
🔧 Debug
advanced
2:00remaining
Why does printing this Django model instance show instead of a custom string?
Given this model:
class Product(models.Model):
    name = models.CharField(max_length=100)

    def str(self):
        return self.name

Why does print(product) show <Product object> instead of the product name?
Django
class Product(models.Model):
    name = models.CharField(max_length=100)

    def str(self):
        return self.name

product = Product(name='Laptop')
print(product)
AThe name attribute is not set correctly
BThe model is missing a __repr__ method
Cprint() cannot print model instances
DThe method should be named __str__, not str
Attempts:
2 left
💡 Hint
Check the method name spelling for string representation.
state_output
advanced
2:00remaining
What is the output of printing a Django model instance with __str__ returning a formatted string?
Consider this model and instance:
class Event(models.Model):
    name = models.CharField(max_length=50)
    date = models.DateField()

    def __str__(self):
        return f"{self.name} on {self.date.strftime('%Y-%m-%d')}"

from datetime import date

event = Event(name='Conference', date=date(2024, 7, 1))
print(event)

What will be printed?
Django
class Event(models.Model):
    name = models.CharField(max_length=50)
    date = models.DateField()

    def __str__(self):
        return f"{self.name} on {self.date.strftime('%Y-%m-%d')}"

from datetime import date

event = Event(name='Conference', date=date(2024, 7, 1))
print(event)
AConference on 2024-07-01
BEvent object
CConference on 07/01/2024
DError: date.strftime not available
Attempts:
2 left
💡 Hint
Check the date formatting inside the __str__ method.
🧠 Conceptual
expert
2:00remaining
Why is defining __str__ important for Django admin and shell usability?
Select the best explanation for why Django models should implement the __str__ method.
AIt provides a readable name for model instances in admin, shell, and logs, improving usability and debugging.
BIt speeds up database queries by caching string values.
CIt automatically creates database indexes on string fields.
DIt prevents Django from raising errors when saving models.
Attempts:
2 left
💡 Hint
Think about how you see model instances in Django admin and shell.