0
0
Djangoframework~20 mins

ManyToManyField for many-to-many in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ManyToManyField Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this Django ORM query involving ManyToManyField?

Given these models:

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

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

And this data:

author1 = Author.objects.create(name='Alice')
author2 = Author.objects.create(name='Bob')
book = Book.objects.create(title='Django Guide')
book.authors.add(author1, author2)

What does book.authors.count() return?

A0
B2
C1
DRaises AttributeError
Attempts:
2 left
💡 Hint

Think about how many authors were added to the book.

📝 Syntax
intermediate
1:30remaining
Which option correctly defines a ManyToManyField in Django?

Choose the correct syntax to define a many-to-many relationship between Student and Course models.

A
class Student(models.Model):
    courses = models.ManyToManyField('Course')
B
class Student(models.Model):
    courses = models.ManyToMany('Course')
C
class Student(models.Model):
    courses = models.ManyToManyField(Course)
D
class Student(models.Model):
    courses = models.ManyToManyField()
Attempts:
2 left
💡 Hint

Remember the exact class name and how to reference related models as strings.

🔧 Debug
advanced
2:00remaining
Why does this ManyToManyField query raise an error?

Given these models:

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

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

And this code:

post = Post.objects.create(title='Hello')
post.tags = Tag.objects.filter(name='django')

Why does this raise an error?

ABecause you cannot assign a queryset directly to a ManyToManyField attribute
BBecause the Tag model does not exist
CBecause the Post instance was not saved before assigning tags
DBecause ManyToManyField requires a list of strings, not objects
Attempts:
2 left
💡 Hint

Think about how to assign multiple related objects to a ManyToManyField.

state_output
advanced
1:30remaining
What is the value of authors_list after this code runs?

Given these models and data:

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

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

alice = Author.objects.create(name='Alice')
bob = Author.objects.create(name='Bob')
book = Book.objects.create(title='Django Tips')
book.authors.add(alice, bob)
authors_list = list(book.authors.values_list('name', flat=True))

What is authors_list?

A[]
B['Django Tips']
C['Alice', 'Bob']
D['Alice']
Attempts:
2 left
💡 Hint

Think about what values_list('name', flat=True) returns.

🧠 Conceptual
expert
2:00remaining
Which statement about Django's ManyToManyField is TRUE?

Choose the correct statement about how Django handles ManyToManyField relationships internally.

ADjango requires manual creation of a join table for every ManyToManyField
BManyToManyField stores all related objects as a JSON list inside a single database field
CManyToManyField relationships are stored as comma-separated strings in the related model's table
DDjango creates an automatic intermediate table to store the many-to-many relationships unless a custom through model is specified
Attempts:
2 left
💡 Hint

Think about how relational databases handle many-to-many relationships.