0
0
Djangoframework~20 mins

Model relationships preview in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Model Relationships 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 accessing related objects in Django?
Given these Django 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, related_name='books')

What will author.books.count() return after creating one author and two books linked to that author?
Django
author = Author.objects.create(name='Alice')
Book.objects.create(title='Book 1', author=author)
Book.objects.create(title='Book 2', author=author)
result = author.books.count()
A1
B2
C0
DRaises AttributeError
Attempts:
2 left
💡 Hint
Think about how the related_name works for reverse lookups.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a ManyToMany relationship in Django?
You want to model a relationship where multiple students can enroll in multiple courses. Which model definition is correct?
A
class Student(models.Model):
    courses = models.OneToOneField('Course', on_delete=models.CASCADE)

class Course(models.Model):
    name = models.CharField(max_length=100)
B
class Student(models.Model):
    courses = models.ForeignKey('Course', on_delete=models.CASCADE)

class Course(models.Model):
    name = models.CharField(max_length=100)
C
class Student(models.Model):
    courses = models.CharField(max_length=100)

class Course(models.Model):
    name = models.CharField(max_length=100)
D
class Student(models.Model):
    courses = models.ManyToManyField('Course')

class Course(models.Model):
    name = models.CharField(max_length=100)
Attempts:
2 left
💡 Hint
Many students can have many courses, so think about the field type that supports many-to-many.
🔧 Debug
advanced
2:00remaining
Why does accessing a related object raise DoesNotExist error?
Given these models:
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField()

And this code:
profile = Profile.objects.get(user=request.user)
print(profile.bio)

Sometimes this raises Profile.DoesNotExist. Why?
AThe user does not have a linked Profile object in the database.
BThe OneToOneField is incorrectly defined and causes a syntax error.
CThe bio field is empty, causing the error.
DThe User model does not exist in Django by default.
Attempts:
2 left
💡 Hint
Think about what happens if the related object is missing.
state_output
advanced
2:00remaining
What is the output of this code using select_related?
Given these models:
class Publisher(models.Model):
    name = models.CharField(max_length=100)

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

And this code:
book = Book.objects.select_related('publisher').get(id=1)
print(book.publisher.name)

What does select_related do here?
AIt raises a ValueError because select_related cannot be used with ForeignKey.
BIt delays fetching the Publisher until publisher.name is accessed, causing a second query.
CIt fetches the Book and its Publisher in one database query, so accessing publisher.name does not cause another query.
DIt fetches only the Book and ignores the Publisher.
Attempts:
2 left
💡 Hint
select_related is used to optimize queries by joining related tables.
🧠 Conceptual
expert
3:00remaining
How does Django handle cascading deletes in model relationships?
Consider these models:
class Category(models.Model):
    name = models.CharField(max_length=100)

class Item(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

If a Category is deleted, what happens to its related Items?
AAll Items linked to the deleted Category are also deleted automatically.
BThe Items remain but their category field is set to NULL.
CDeleting Category raises an IntegrityError because Items depend on it.
DThe Items are archived but not deleted.
Attempts:
2 left
💡 Hint
Check the meaning of on_delete=models.CASCADE.