0
0
Djangoframework~20 mins

Through model for extra fields on M2M in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Through Model 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 extra fields in a through model?

Consider a Django many-to-many relationship with a through model that adds an extra field date_joined. What will be the output of the following code snippet?

Django
class Membership(models.Model):
    person = models.ForeignKey('Person', on_delete=models.CASCADE)
    group = models.ForeignKey('Group', on_delete=models.CASCADE)
    date_joined = models.DateField()

class Person(models.Model):
    name = models.CharField(max_length=100)
    groups = models.ManyToManyField('Group', through='Membership')

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

# Assume we have a person instance and group instance linked via Membership
membership = Membership.objects.get(person=person_instance, group=group_instance)
print(membership.date_joined)
ARaises DoesNotExist error because Membership is not accessible directly
BPrints the date_joined value stored in the Membership instance
CRaises AttributeError because date_joined is not on Person or Group
DPrints the current date because date_joined defaults to today
Attempts:
2 left
💡 Hint

Remember that the through model stores extra fields and can be queried directly.

state_output
intermediate
1:30remaining
How many Membership records exist after adding a person to a group with extra fields?

Given the following models with a through model Membership that has an extra field role, what is the number of Membership records after executing the code below?

Django
person = Person.objects.create(name='Alice')
group = Group.objects.create(name='Developers')
Membership.objects.create(person=person, group=group, role='admin')
print(Membership.objects.count())
A1
B0
C2
DRaises IntegrityError due to missing fields
Attempts:
2 left
💡 Hint

Creating a Membership instance adds one record.

🔧 Debug
advanced
2:00remaining
Why does this code raise an error when adding to a ManyToManyField with a through model?

Given the models below, why does the code person.groups.add(group_instance) raise an error?

Django
class Membership(models.Model):
    person = models.ForeignKey('Person', on_delete=models.CASCADE)
    group = models.ForeignKey('Group', on_delete=models.CASCADE)
    date_joined = models.DateField()

class Person(models.Model):
    name = models.CharField(max_length=100)
    groups = models.ManyToManyField('Group', through='Membership')

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

person = Person.objects.create(name='Bob')
group_instance = Group.objects.create(name='Admins')
person.groups.add(group_instance)
ABecause person.groups is not a valid attribute
BBecause the group_instance is not saved to the database
CBecause the through model requires extra fields, add() cannot be used without them
DBecause the ManyToManyField is missing related_name
Attempts:
2 left
💡 Hint

Think about how Django handles many-to-many relations with extra fields.

📝 Syntax
advanced
2:30remaining
Which option correctly defines a through model with an extra field for a ManyToManyField?

Choose the correct Django model code that defines a many-to-many relationship with a through model containing an extra field status.

A
class Membership(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    status = models.CharField(max_length=20)

class User(models.Model):
    groups = models.ManyToManyField(Group)
B
class Membership(models.Model):
    user = models.ManyToManyField(User)
    group = models.ManyToManyField(Group)
    status = models.CharField(max_length=20)

class User(models.Model):
    groups = models.ManyToManyField(Group, through='Membership')
C
class Membership(models.Model):
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)
    status = models.CharField(max_length=20)

class User(models.Model):
    groups = models.ManyToManyField(Group, through='Membership')
D
class Membership(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    status = models.CharField(max_length=20)

class User(models.Model):
    groups = models.ManyToManyField(Group, through='Membership')
Attempts:
2 left
💡 Hint

Remember that ForeignKey fields in the through model require on_delete argument.

🧠 Conceptual
expert
1:30remaining
What is the main advantage of using a through model with extra fields on a ManyToManyField?

Why would a developer choose to use a through model with extra fields instead of a simple ManyToManyField without a through model?

ATo store additional information about the relationship between the two models, such as timestamps or roles
BTo improve database query performance by avoiding joins
CTo automatically create reverse relationships without extra code
DTo allow ManyToManyField to accept more than two models
Attempts:
2 left
💡 Hint

Think about what extra fields in the through model represent.