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
Recall & Review
beginner
What is a ForeignKey in Django?
A ForeignKey in Django is a field used to create a one-to-many relationship between two models. It links one model to another by storing the primary key of the related model.
Click to reveal answer
beginner
How do you define a one-to-many relationship using ForeignKey in Django models?
You add a ForeignKey field in the 'many' side model pointing to the 'one' side model. For example, in a Blog and Entry setup, Entry has a ForeignKey to Blog.
Click to reveal answer
intermediate
What does the 'on_delete' parameter do in a ForeignKey field?
The 'on_delete' parameter defines what happens to related objects when the referenced object is deleted. Common options are CASCADE (delete related), SET_NULL (set to null), and PROTECT (prevent deletion).
Click to reveal answer
intermediate
How can you access all related objects from the 'one' side in a one-to-many ForeignKey relationship?
You use the reverse relation manager. By default, Django creates a manager named <modelname>_set. For example, blog.entry_set.all() returns all entries for a blog.
Click to reveal answer
beginner
Why is using ForeignKey better than storing IDs manually for relationships?
ForeignKey ensures data integrity, provides easy querying, and integrates with Django's ORM features like automatic joins and reverse lookups, making code cleaner and safer.
Click to reveal answer
In Django, which model should have the ForeignKey field in a one-to-many relationship?
AThe model on the 'many' side
BThe model on the 'one' side
CBoth models
DNeither model
✗ Incorrect
The ForeignKey is placed on the 'many' side model to link it to the 'one' side model.
What does the 'on_delete=models.CASCADE' option do in a ForeignKey?
APrevents deletion of the referenced object
BDeletes related objects when the referenced object is deleted
CSets the ForeignKey to null on deletion
DDoes nothing on deletion
✗ Incorrect
CASCADE deletes all related objects automatically when the referenced object is deleted.
How do you access all related objects from the 'one' side in a ForeignKey relationship?
AYou cannot access related objects from the 'one' side
BUsing ForeignKey field directly
CUsing <modelname>_set manager
DUsing a separate query unrelated to ForeignKey
✗ Incorrect
Django automatically creates a reverse manager named _set to access related objects.
Which of these is NOT a valid 'on_delete' option in Django ForeignKey?
AIGNORE
BCASCADE
CPROTECT
DSET_NULL
✗ Incorrect
'IGNORE' is not a valid on_delete option in Django.
Why is using ForeignKey preferred over manually storing IDs for relationships?
AIt requires more manual code
BIt makes the database slower
CIt disables reverse lookups
DIt ensures data integrity and easier querying
✗ Incorrect
ForeignKey helps maintain data integrity and provides easy querying and reverse lookups.
Explain how to create a one-to-many relationship using ForeignKey in Django models.
Think about which model holds the ForeignKey and what happens on deletion.
You got /4 concepts.
Describe how to access related objects from the 'one' side in a ForeignKey relationship in Django.
Remember Django creates a manager automatically for reverse lookups.
You got /4 concepts.
Practice
(1/5)
1. In Django, where should you place a ForeignKey field to represent a one-to-many relationship?
easy
A. In the model representing the 'many' side of the relationship
B. In the model representing the 'one' side of the relationship
C. In both models to link them together
D. You don't use ForeignKey for one-to-many relationships
Solution
Step 1: Understand the one-to-many relationship
One object on the 'one' side can relate to many objects on the 'many' side.
Step 2: Place ForeignKey on the 'many' side
The ForeignKey field goes in the model that represents the 'many' side to link back to the 'one' side.
Final Answer:
In the model representing the 'many' side of the relationship -> Option A
Quick Check:
ForeignKey on 'many' side = A [OK]
Hint: ForeignKey always goes on the 'many' side model [OK]
Common Mistakes:
Putting ForeignKey on the 'one' side model
Adding ForeignKey to both models
Thinking ForeignKey is not used for one-to-many
2. Which of the following is the correct syntax to define a ForeignKey in a Django model named Book that links to a model named Author?
easy
A. author = models.ForeignKey(Author, delete=models.CASCADE)
B. author = models.ForeignKey(Author, on_delete=models.CASCADE)
C. author = models.ForeignKey('Author')
D. author = models.ForeignKey(Author, on_delete='CASCADE')
Solution
Step 1: Check ForeignKey syntax
ForeignKey requires the related model and the on_delete argument to specify delete behavior.
Step 2: Validate correct usage
author = models.ForeignKey(Author, on_delete=models.CASCADE) correctly uses on_delete=models.CASCADE. Options A, B, and D have syntax errors or missing required arguments.
Final Answer:
author = models.ForeignKey(Author, on_delete=models.CASCADE) -> Option B
Quick Check:
Correct ForeignKey syntax = C [OK]
Hint: Always include on_delete=models.CASCADE with ForeignKey [OK]
Common Mistakes:
Omitting on_delete argument
Using wrong argument name like delete
Passing on_delete as string instead of constant
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.objects.filter(author__name='Alice').count() return if there are 3 books by Alice and 2 by Bob?
medium
A. 0
B. 2
C. 5
D. 3
Solution
Step 1: Understand the filter query
The query filters books where the related author's name is 'Alice'.
Step 2: Count matching books
Since 3 books belong to Alice, the count will be 3.
Final Answer:
3 -> Option D
Quick Check:
Books by Alice = 3 [OK]
Hint: Filter on related model with double underscore __ [OK]
Common Mistakes:
Counting authors instead of books
Using single underscore instead of double
Confusing author name with book title
4. What is wrong with this Django model code?
class Comment(models.Model):
post = models.ForeignKey(Post)
text = models.TextField()
medium
A. TextField cannot be used for text
B. ForeignKey should be named post_id
C. Missing on_delete argument in ForeignKey
D. ForeignKey should be in Post model
Solution
Step 1: Check ForeignKey arguments
Since Django 2.0, on_delete is required for ForeignKey fields.
Step 2: Identify missing on_delete
The code lacks on_delete, causing an error.
Final Answer:
Missing on_delete argument in ForeignKey -> Option C
Quick Check:
ForeignKey requires on_delete argument [OK]
Hint: Always add on_delete to ForeignKey fields [OK]
Common Mistakes:
Forgetting on_delete causes errors
Renaming ForeignKey field incorrectly
Thinking TextField is invalid for text
5. You have these models:
class Category(models.Model):
name = models.CharField(max_length=50)
class Product(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
If a Category is deleted, what happens to its related Products?
hard
A. Products remain but their category field is set to NULL
B. Products are deleted automatically
C. Deletion of Category is blocked if Products exist
D. Products keep the deleted category reference
Solution
Step 1: Understand on_delete=models.SET_NULL
This option sets the ForeignKey field to NULL when the related object is deleted.
Step 2: Apply to Product-Category relation
When a Category is deleted, related Products keep their records but their category field becomes NULL.
Final Answer:
Products remain but their category field is set to NULL -> Option A
Quick Check:
on_delete=SET_NULL means keep products, nullify category [OK]
Hint: SET_NULL keeps related objects, clears ForeignKey [OK]