Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does a ManyToManyField in Django represent?
easy
A. A field used to store file uploads
B. A relationship where one record relates to only one record in another model
C. A field that stores a single value like a string or number
D. A relationship where many records in one model relate to many records in another model

Solution

  1. Step 1: Understand relationship types in Django models

    Django uses different fields to represent relationships: OneToOne, ForeignKey (one-to-many), and ManyToMany.
  2. Step 2: Identify ManyToManyField purpose

    ManyToManyField connects many records from one model to many records in another, allowing multiple links both ways.
  3. Final Answer:

    A relationship where many records in one model relate to many records in another model -> Option D
  4. Quick Check:

    ManyToManyField = many-to-many relation [OK]
Hint: ManyToManyField links many items to many items [OK]
Common Mistakes:
  • Confusing ManyToManyField with ForeignKey
  • Thinking it stores single values
  • Assuming it stores files
2. Which of the following is the correct way to define a many-to-many relationship in a Django model?
easy
A. friends = models.ManyToManyField('self')
B. friends = models.ForeignKey('self', on_delete=models.CASCADE)
C. friends = models.OneToOneField('self', on_delete=models.CASCADE)
D. friends = models.CharField(max_length=100)

Solution

  1. Step 1: Review Django field types for relationships

    ForeignKey is for one-to-many, OneToOneField for one-to-one, ManyToManyField for many-to-many.
  2. Step 2: Check syntax for many-to-many self-referential field

    Using ManyToManyField with 'self' allows a model to relate to itself many-to-many, syntax is correct as in friends = models.ManyToManyField('self').
  3. Final Answer:

    friends = models.ManyToManyField('self') -> Option A
  4. Quick Check:

    ManyToManyField syntax = friends = models.ManyToManyField('self') [OK]
Hint: ManyToManyField uses models.ManyToManyField('ModelName') [OK]
Common Mistakes:
  • Using ForeignKey instead of ManyToManyField
  • Missing quotes around model name
  • Using CharField for relationships
3. 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)

# Assume authors and books are created and linked properly
book = Book.objects.get(title='Django Guide')
authors = book.authors.all()

What does authors contain?
medium
A. A list of book titles
B. A QuerySet of Author objects linked to the book 'Django Guide'
C. A single Author object
D. An error because ManyToManyField cannot be queried

Solution

  1. Step 1: Understand ManyToManyField query behavior

    Accessing book.authors.all() returns a QuerySet of all Author objects related to that Book.
  2. Step 2: Identify what authors holds

    It holds multiple Author instances linked to the book, not a single object or unrelated data.
  3. Final Answer:

    A QuerySet of Author objects linked to the book 'Django Guide' -> Option B
  4. Quick Check:

    ManyToManyField.all() returns QuerySet [OK]
Hint: ManyToManyField.all() returns related objects QuerySet [OK]
Common Mistakes:
  • Expecting a single object instead of QuerySet
  • Thinking it returns unrelated data
  • Assuming it causes an error
4. What is wrong with this Django model code?
class Student(models.Model):
    name = models.CharField(max_length=50)
    courses = models.ManyToManyField('Course')

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

# Usage
student = Student(name='Alice')
student.courses.add(1)
medium
A. You must call save() before adding to ManyToManyField
B. ManyToManyField cannot be used between Student and Course
C. You cannot add an integer directly; you must add a Course instance
D. The models should be swapped in order

Solution

  1. Step 1: Check how to add related objects to ManyToManyField

    The add() method requires the parent instance (Student) to be saved first.
  2. Step 2: Identify the issue in the code

    student = Student(name='Alice') creates an unsaved instance; call student.save() before student.courses.add(1).
  3. Final Answer:

    You must call save() before adding to ManyToManyField -> Option A
  4. Quick Check:

    Save instance before ManyToManyField.add() [OK]
Hint: Save the model instance before calling add() on ManyToManyField [OK]
Common Mistakes:
  • Adding raw integers without existing related objects
  • Confusing ForeignKey add with ManyToManyField add
  • Not saving objects before adding relations
5. You want to model a social app where users can follow many other users and be followed by many users. Which is the best way to define this using Django's ManyToManyField?
hard
A. class User(models.Model): name = models.CharField(max_length=100) follows = models.ForeignKey('self', on_delete=models.CASCADE)
B. class User(models.Model): name = models.CharField(max_length=100) follows = models.ManyToManyField('self')
C. class User(models.Model): name = models.CharField(max_length=100) follows = models.ManyToManyField('self', symmetrical=False, related_name='followers')
D. class User(models.Model): name = models.CharField(max_length=100) follows = models.OneToOneField('self', on_delete=models.CASCADE)

Solution

  1. Step 1: Understand self-referential many-to-many relationships

    Users following other users is a many-to-many relationship with direction (not symmetrical). So symmetrical=False is needed.
  2. Step 2: Check options for correct field and parameters

    class User(models.Model): name = models.CharField(max_length=100) follows = models.ManyToManyField('self', symmetrical=False, related_name='followers') uses ManyToManyField('self', symmetrical=False) with related_name='followers' to access reverse relation, which fits the social follow model.
  3. Final Answer:

    Use ManyToManyField('self', symmetrical=False, related_name='followers') -> Option C
  4. Quick Check:

    Self ManyToManyField with symmetrical=False for follows [OK]
Hint: Use symmetrical=False for directed self ManyToManyField [OK]
Common Mistakes:
  • Using ForeignKey or OneToOneField for many-to-many
  • Missing symmetrical=False for directed relations
  • Not setting related_name for reverse access