Challenge - 5 Problems
String Representation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of printing a Django model instance with __str__ defined?
Consider this Django model:
What will
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)
Attempts:
2 left
💡 Hint
The __str__ method defines what print(instance) shows.
✗ Incorrect
The __str__ method returns a string that print() uses. Here it returns the title and author combined.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
The __str__ method must return a string, not print it or call attributes incorrectly.
✗ Incorrect
Option B returns the name attribute correctly. Option B prints but returns None. Option B uses undefined name variable. Option B calls name as a method which is invalid.
🔧 Debug
advanced2:00remaining
Why does printing this Django model instance show instead of a custom string?
Given this model:
Why does
class Product(models.Model):
name = models.CharField(max_length=100)
def str(self):
return self.nameWhy 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)
Attempts:
2 left
💡 Hint
Check the method name spelling for string representation.
✗ Incorrect
Django uses __str__ method to get string representation. Here the method is named str without underscores, so it is ignored.
❓ state_output
advanced2:00remaining
What is the output of printing a Django model instance with __str__ returning a formatted string?
Consider this model and instance:
What will be printed?
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)
Attempts:
2 left
💡 Hint
Check the date formatting inside the __str__ method.
✗ Incorrect
The __str__ method formats the date as 'YYYY-MM-DD' using strftime, so the output is 'Conference on 2024-07-01'.
🧠 Conceptual
expert2: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.
Attempts:
2 left
💡 Hint
Think about how you see model instances in Django admin and shell.
✗ Incorrect
Without __str__, Django shows generic object names which are hard to identify. Defining __str__ makes instances show meaningful names.