0
0
Djangoframework~20 mins

ForeignKey for one-to-many in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ForeignKey Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django model relationship produce?
Given these Django models, what will be the output of print(book.author.name) if book is an instance of Book linked to an Author named 'Alice'?
Django
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

# Assume book is a Book instance linked to Author named 'Alice'
print(book.author.name)
ARaises AttributeError because 'author' is not accessible
B"Alice"
C"book.author"
DNone
Attempts:
2 left
💡 Hint
Remember that ForeignKey creates a link to a single related object.
state_output
intermediate
2:00remaining
How many books are related to this author?
Given these models and an Author instance author with 3 related Book instances, what is the output of print(author.book_set.count())?
Django
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

# Assume author is an Author instance with 3 books linked
print(author.book_set.count())
ARaises AttributeError because 'book_set' does not exist
B0
C3
DRaises TypeError
Attempts:
2 left
💡 Hint
Django automatically creates a reverse relation named modelname_set.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a ForeignKey for one-to-many in Django?
Select the correct Django model field definition to create a one-to-many relationship from Book to Author.
Django
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    # ForeignKey field here
Aauthor = models.ForeignKey(Author, on_delete=models.CASCADE)
Bauthor = models.CharField(max_length=100)
Cauthor = models.ManyToManyField(Author)
Dauthor = models.OneToOneField(Author, on_delete=models.CASCADE)
Attempts:
2 left
💡 Hint
One-to-many uses ForeignKey, not OneToOneField or ManyToManyField.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
Given these models, why does accessing book.author_set.all() raise an error?
Django
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

book = Book.objects.first()
print(book.author_set.all())
A'Author' object has no attribute 'author_set' because of typo
BNone, code runs without error
CSyntaxError due to missing parentheses in ForeignKey
D'Book' object has no attribute 'author_set' because reverse relation is on Author, not Book
Attempts:
2 left
💡 Hint
Reverse relations are accessed from the model on the 'one' side, not the 'many' side.
🧠 Conceptual
expert
3:00remaining
What happens when you delete an Author instance linked to Books with on_delete=models.CASCADE?
Consider these models with author = models.ForeignKey(Author, on_delete=models.CASCADE). What is the effect on related Book instances when an Author is deleted?
Django
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
AAll Book instances linked to the deleted Author are also deleted
BBook instances remain but their author field is set to NULL
CDeletion of Author is blocked if any Book is linked
DBook instances remain unchanged with author field still pointing to deleted Author
Attempts:
2 left
💡 Hint
on_delete=models.CASCADE means related objects are deleted too.