A ForeignKey creates a many-to-one relationship between two models. It means each record in the model with the ForeignKey points to one record in another model, like a child pointing to a parent.
Click to reveal answer
beginner
Explain ManyToManyField in Django models.
ManyToManyField creates a many-to-many relationship. It means records in one model can relate to many records in another model, and vice versa, like friends who can have many friends.
Click to reveal answer
beginner
What does OneToOneField represent in Django?
OneToOneField creates a one-to-one relationship between two models. Each record in one model matches exactly one record in another, like a person and their unique passport.
Click to reveal answer
intermediate
How does Django handle reverse relationships automatically?
Django automatically creates reverse relationships so you can access related objects from either side. For example, from a parent you can get all children without extra code.
Click to reveal answer
intermediate
Why use related_name in Django model relationships?
related_name sets the attribute name for reverse lookups. It helps avoid clashes and makes code clearer when accessing related objects from the other side.
Click to reveal answer
Which Django field type would you use to link a blog post to a single author?
AForeignKey
BManyToManyField
COneToOneField
DCharField
✗ Incorrect
A ForeignKey links many posts to one author, representing a many-to-one relationship.
If you want to model students and courses where each student can take many courses and each course can have many students, which field is best?
AForeignKey
BOneToOneField
CManyToManyField
DIntegerField
✗ Incorrect
ManyToManyField models many-to-many relationships like students and courses.
What does OneToOneField ensure in Django models?
AOne record links to exactly one record
BMany records link to many records
CMultiple records link to one record
DNo relationship
✗ Incorrect
OneToOneField ensures a strict one-to-one link between two records.
What is the purpose of the related_name attribute in Django relationships?
ATo set the field's max length
BTo set the database table name
CTo define the primary key
DTo name the reverse relation attribute
✗ Incorrect
related_name defines the attribute name used to access related objects from the reverse side.
How can you access all related objects from the reverse side of a ForeignKey?
AUsing the model's primary key
BUsing the related_name or default attribute
CUsing a ManyToManyField
DUsing a OneToOneField
✗ Incorrect
Django creates a reverse attribute named by related_name or a default to access related objects.
Describe the three main types of model relationships in Django and give a simple real-life example for each.
Think about parents and children, friends, and unique pairs like person and passport.
You got /4 concepts.
Explain how Django's related_name helps when working with model relationships and why it is useful.
Consider how you access related objects from the other side.
You got /4 concepts.
Practice
(1/5)
1. Which Django model field type represents a many-to-one relationship?
easy
A. ForeignKey
B. OneToOneField
C. ManyToManyField
D. CharField
Solution
Step 1: Understand relationship types in Django models
Django uses ForeignKey for many-to-one, OneToOneField for one-to-one, and ManyToManyField for many-to-many relationships.
Step 2: Match the field to many-to-one relationship
The ForeignKey field connects many instances of one model to a single instance of another model, representing many-to-one.
Final Answer:
ForeignKey -> Option A
Quick Check:
Many-to-one = ForeignKey [OK]
Hint: ForeignKey always means many-to-one relation [OK]
Common Mistakes:
Confusing OneToOneField with ForeignKey
Thinking ManyToManyField is many-to-one
Using CharField for relationships
2. Which of the following is the correct syntax to define a one-to-one relationship in a Django model?
easy
A. user = models.ForeignKey(User, on_delete=models.CASCADE)
B. email = models.CharField(max_length=100)
C. groups = models.ManyToManyField(Group)
D. profile = models.OneToOneField(User, on_delete=models.CASCADE)
Solution
Step 1: Identify the field for one-to-one relationship
The OneToOneField is used to create a one-to-one link between two models.
Step 2: Check the syntax correctness
The syntax profile = models.OneToOneField(User, on_delete=models.CASCADE) correctly defines a one-to-one relationship with proper arguments.
Final Answer:
profile = models.OneToOneField(User, on_delete=models.CASCADE) -> Option D
Hint: OneToOneField uses on_delete argument like ForeignKey [OK]
Common Mistakes:
Using ForeignKey instead of OneToOneField
Omitting on_delete argument
Confusing ManyToManyField syntax
3. Given these 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)
What will book.author.name return if book is a Book instance?
medium
A. The name of the author related to the book
B. The title of the book
C. An error because author is a ForeignKey
D. The id of the author
Solution
Step 1: Understand ForeignKey access in Django
A ForeignKey field allows access to the related model instance. So book.author gives the Author instance linked to the book.
Step 2: Access the related model's attribute
Using book.author.name accesses the name attribute of the related Author instance, returning the author's name.
Final Answer:
The name of the author related to the book -> Option A
Quick Check:
ForeignKey attribute access returns related model data [OK]
Hint: ForeignKey lets you access related object attributes directly [OK]
Common Mistakes:
Expecting book.author to be just an ID
Thinking book.author.name causes error
Confusing book.title with author name
4. Identify the error in this Django model relationship:
class Profile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField()
medium
A. OneToOneField should be ForeignKey
B. Missing on_delete argument in OneToOneField
C. TextField cannot be used for bio
D. User model must be imported
Solution
Step 1: Check OneToOneField required arguments
Django requires the on_delete argument for relationship fields like OneToOneField to specify behavior on deletion.
Step 2: Identify missing argument
The model misses on_delete=models.CASCADE or similar, which causes an error.
Final Answer:
Missing on_delete argument in OneToOneField -> Option B
Quick Check:
Relationship fields need on_delete argument [OK]
Hint: Always add on_delete to OneToOneField and ForeignKey [OK]
Common Mistakes:
Omitting on_delete argument
Confusing OneToOneField with ForeignKey
Assuming TextField is invalid for bio
5. You want to model a social app where users can follow many other users and be followed by many users. Which Django model field setup correctly represents this many-to-many self-relationship?
hard
A. followers = models.OneToOneField('self', on_delete=models.CASCADE)
B. followers = models.ForeignKey('self', on_delete=models.CASCADE)
C. followers = models.ManyToManyField('self', symmetrical=False, related_name='following')
D. followers = models.ManyToManyField(User)
Solution
Step 1: Understand many-to-many self-relationship
Users can follow many users and be followed by many, so a many-to-many relationship to self is needed.
Step 2: Check correct ManyToManyField syntax for self
Using ManyToManyField('self', symmetrical=False, related_name='following') correctly models directed follow relationships.
Final Answer:
followers = models.ManyToManyField('self', symmetrical=False, related_name='following') -> Option C
Quick Check:
ManyToManyField to 'self' with symmetrical=False models follow [OK]
Hint: Use ManyToManyField('self', symmetrical=False) for directed self-relations [OK]
Common Mistakes:
Using ForeignKey or OneToOneField for many-to-many
Omitting symmetrical=False for directed relation
Using ManyToManyField(User) instead of 'self'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.