0
0
Djangoframework~20 mins

Why relationships model real-world data in Django - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Relationships Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding One-to-Many Relationships in Django Models

Consider a Django app where each Author can write multiple Books. How does Django model this real-world relationship?

What is the correct way to represent this in Django models?

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

class Book(models.Model):
    title = models.CharField(max_length=100)
    # What field links Book to Author?
Aauthor = models.ForeignKey(Author, on_delete=models.CASCADE)
Bauthor = models.ManyToManyField(Author)
Cauthor = models.OneToOneField(Author, on_delete=models.CASCADE)
Dauthor = models.CharField(max_length=100)
Attempts:
2 left
💡 Hint

Think about how many books one author can write and how Django links models.

component_behavior
intermediate
2:00remaining
How Many-to-Many Relationships Work in Django

In a Django app, Students can enroll in many Courses, and each course can have many students.

What will be the output of student.courses.all() if courses is a ManyToManyField on Student?

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

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

# Assume student is a Student instance with 2 courses enrolled
print(student.courses.all())
AAttributeError: 'Student' object has no attribute 'courses'
B<QuerySet []>
CTypeError: 'ManyToManyField' object is not iterable
D<QuerySet [<Course: Course1>, <Course: Course2>]>
Attempts:
2 left
💡 Hint

Remember what ManyToManyField returns when you call all().

📝 Syntax
advanced
2:00remaining
Identify the Error in a Django OneToOneField Declaration

What error will this Django model code raise?

Django
class Profile(models.Model):
    user = models.OneToOneField(User)
    bio = models.TextField()
ANo error, code runs fine
BSyntaxError: invalid syntax
CTypeError: __init__() missing 1 required positional argument: 'on_delete'
DAttributeError: 'OneToOneField' object has no attribute 'on_delete'
Attempts:
2 left
💡 Hint

Check the required arguments for OneToOneField in Django 3.12+.

state_output
advanced
2:00remaining
What is the Output of Accessing Related Objects in Django?

Given these models, what will author.book_set.count() output?

Django
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())
A3
B0
CAttributeError: 'Author' object has no attribute 'book_set'
DTypeError: 'RelatedManager' object is not callable
Attempts:
2 left
💡 Hint

Remember the default related name for ForeignKey reverse lookups.

🔧 Debug
expert
2:00remaining
Debugging a ManyToManyField Access Error in Django

Why does this code raise an error?

Django
class Tag(models.Model):
    name = models.CharField(max_length=50)

class Post(models.Model):
    title = models.CharField(max_length=100)
    tags = models.ManyToManyField(Tag)

post = Post.objects.first()
print(post.tags)
AAttributeError: 'ManyToManyField' object has no attribute 'all'
BManyRelatedManager object printed as <ManyRelatedManager: tags>
CTypeError: ManyRelatedManager object is not iterable
D<ManyRelatedManager: tags>
Attempts:
2 left
💡 Hint

Think about what printing a ManyToManyField directly shows.