Challenge - 5 Problems
Related Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the reverse access name for a ForeignKey with related_name='children'?
Given the following Django models, what is the correct way to access all children of a Parent instance using the related_name?
class Parent(models.Model):
name = models.CharField(max_length=100)
class Child(models.Model):
parent = models.ForeignKey(Parent, related_name='children', on_delete=models.CASCADE)
name = models.CharField(max_length=100)Attempts:
2 left
💡 Hint
Think about the related_name attribute and how it changes the default reverse accessor.
✗ Incorrect
The related_name='children' sets the reverse accessor from Parent to Child as 'children'. So, to get all children of a parent, you use parent.children.all(). The default would be parent.child_set.all() if related_name was not set.
📝 Syntax
intermediate2:00remaining
Which related_name causes a syntax error in this ForeignKey?
Identify which related_name value will cause a syntax error when used in this Django model:
class Book(models.Model):
author = models.ForeignKey('Author', related_name=___, on_delete=models.CASCADE)
title = models.CharField(max_length=200)Attempts:
2 left
💡 Hint
related_name must be a valid Python identifier.
✗ Incorrect
The related_name must be a valid Python attribute name. Hyphens '-' are not allowed in Python identifiers, so 'books-written' causes a syntax error. The others are valid identifiers.
❓ state_output
advanced2:00remaining
What is the output of accessing reverse relation with related_name?
Consider these Django models and code:
What will be printed?
class Category(models.Model):
name = models.CharField(max_length=50)
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=100)
cat = Category.objects.create(name='Toys')
Product.objects.create(name='Car', category=cat)
Product.objects.create(name='Doll', category=cat)
print(list(cat.products.values_list('name', flat=True)))What will be printed?
Attempts:
2 left
💡 Hint
The related_name 'products' allows reverse access from Category to Product.
✗ Incorrect
The related_name 'products' lets us access all Product instances linked to the Category instance 'cat'. So cat.products.all() returns both products. The values_list with 'name' and flat=True returns a list of product names.
🔧 Debug
advanced2:00remaining
Why does this reverse access raise AttributeError?
Given these models:
And this code:
Why does this raise AttributeError: 'Author' object has no attribute 'books'?
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=200)And this code:
author = Author.objects.first() print(author.books.all())
Why does this raise AttributeError: 'Author' object has no attribute 'books'?
Attempts:
2 left
💡 Hint
Check the default reverse accessor name when related_name is not set.
✗ Incorrect
When related_name is not set, Django uses the default reverse accessor as modelname_set, here 'book_set'. So author.books does not exist, but author.book_set does.
🧠 Conceptual
expert3:00remaining
How does related_name affect reverse access in multi-level relations?
Given these models:
Which expression correctly gets all Person instances living in a Country instance named 'country'?
class Country(models.Model):
name = models.CharField(max_length=50)
class City(models.Model):
country = models.ForeignKey(Country, related_name='cities', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
class Person(models.Model):
city = models.ForeignKey(City, related_name='residents', on_delete=models.CASCADE)
name = models.CharField(max_length=100)Which expression correctly gets all Person instances living in a Country instance named 'country'?
Attempts:
2 left
💡 Hint
Think about how to traverse ForeignKey relations in Django queries.
✗ Incorrect
To get all Person instances living in a Country, you filter Person by city__country=country. The related_name allows reverse access from Country to City as 'cities', and from City to Person as 'residents', but you cannot chain reverse relations directly as attributes without querying.