0
0
Djangoframework~20 mins

Related name for reverse access in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Related Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
Aparent.children.all()
Bchild.parent.all()
Cparent.child_set.all()
Dparent.children_set.all()
Attempts:
2 left
💡 Hint
Think about the related_name attribute and how it changes the default reverse accessor.
📝 Syntax
intermediate
2: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)
A'books_written'
B'books123'
C'books-written'
D'booksWritten'
Attempts:
2 left
💡 Hint
related_name must be a valid Python identifier.
state_output
advanced
2:00remaining
What is the output of accessing reverse relation with related_name?
Consider these Django models and code:

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?
A[]
B['Car', 'Doll']
C['Car']
D['Toys']
Attempts:
2 left
💡 Hint
The related_name 'products' allows reverse access from Category to Product.
🔧 Debug
advanced
2:00remaining
Why does this reverse access raise AttributeError?
Given these models:

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'?
ABecause the ForeignKey has no related_name, so default is 'book_set', not 'books'.
BBecause the author variable is None and has no attributes.
CBecause the Book model is missing a related_name attribute.
DBecause the Author model must define a reverse relation explicitly.
Attempts:
2 left
💡 Hint
Check the default reverse accessor name when related_name is not set.
🧠 Conceptual
expert
3:00remaining
How does related_name affect reverse access in multi-level relations?
Given these models:

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'?
Acountry.cities.residents_set.all()
Bcountry.residents.all()
Ccountry.cities.residents.all()
DPerson.objects.filter(city__country=country)
Attempts:
2 left
💡 Hint
Think about how to traverse ForeignKey relations in Django queries.