Recall & Review
beginner
What is a self-referencing relationship in Django models?
A self-referencing relationship is when a model has a field that links to itself, allowing instances to relate to other instances of the same model.
Click to reveal answer
beginner
How do you define a self-referencing ForeignKey in Django?
Use ForeignKey with 'self' as the model name, like: <br>
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)Click to reveal answer
intermediate
Why might you use
null=True and blank=True in a self-referencing field?Because some instances may not have a related instance (like a root node), allowing the field to be empty is necessary.
Click to reveal answer
beginner
What is a common use case for self-referencing relationships?
Modeling hierarchical data like categories, organizational charts, or threaded comments where items relate to others of the same type.
Click to reveal answer
intermediate
How can you access related objects in a self-referencing relationship?
Django automatically creates a reverse relation you can access using the related_name or default
modelname_set attribute.Click to reveal answer
In Django, how do you specify a self-referencing ForeignKey?
✗ Incorrect
To create a self-referencing ForeignKey, you use 'self' as the model name.
Why would you add
null=True to a self-referencing ForeignKey?✗ Incorrect
null=True allows the field to be empty, useful for instances without a parent.
Which of these is a typical example of a self-referencing relationship?
✗ Incorrect
An employee linked to their manager is a self-referencing relationship because both are in the same model.
What does Django create automatically for reverse access in self-referencing ForeignKey?
✗ Incorrect
Django creates a reverse relation accessible via
modelname_set or a custom related_name.Which argument helps you customize the reverse relation name in a self-referencing ForeignKey?
✗ Incorrect
related_name sets the name for reverse access to related objects.
Explain how to create a self-referencing relationship in a Django model and why you might want to use it.
Think about linking an item to another item of the same type.
You got /4 concepts.
Describe how Django handles reverse relations in self-referencing ForeignKey fields and how you can customize it.
Consider how you get all children or related items from a parent.
You got /4 concepts.