0
0
Djangoframework~20 mins

Self-referencing relationships in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Self-Referencing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django model's __str__ method?
Consider this Django model with a self-referencing ForeignKey. What will be printed when calling print(category) if category.name = 'Books' and category.parent = None?
Django
from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100)
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.name} - Parent: {self.parent.name if self.parent else 'None'}"
ABooks - Parent: None
BBooks - Parent: Books
CBooks - Parent: ''
DBooks - Parent: Null
Attempts:
2 left
💡 Hint
Check how the __str__ method handles the parent attribute when it is None.
state_output
intermediate
2:00remaining
What is the value of child.parent.name after saving these objects?
Given this Django model and code, what is the value of child.parent.name after execution?
Django
from django.db import models

class Node(models.Model):
    name = models.CharField(max_length=50)
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)

root = Node.objects.create(name='Root')
child = Node.objects.create(name='Child', parent=root)

result = child.parent.name
ARoot
BChild
CNone
DRaises AttributeError
Attempts:
2 left
💡 Hint
Look at how the parent is assigned when creating the child node.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a self-referencing ManyToManyField in Django?
You want to create a Django model where each person can have many friends, and friends are also Person instances. Which code snippet correctly defines this self-referencing relationship?
Django
from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)
    friends = ???
Amodels.ManyToManyField('self', symmetrical=False, blank=True)
Bmodels.ManyToManyField('Person', symmetrical=False)
Cmodels.ManyToManyField('self', symmetrical=True, blank=True)
Dmodels.ForeignKey('self', on_delete=models.CASCADE)
Attempts:
2 left
💡 Hint
Friends usually have a symmetrical relationship.
🔧 Debug
advanced
2:30remaining
Why does this self-referencing model raise a ValueError?
This Django model raises a ValueError when running migrations. What is the cause?
Django
from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=100)
    manager = models.ForeignKey('Employee', null=True, blank=True, on_delete=models.SET_NULL)
AThe model must inherit from models.SelfReferencingModel
BForeignKey cannot be null or blank in self-referencing models
Con_delete=models.SET_NULL is invalid for self-referencing fields
DThe ForeignKey must use a string 'self' instead of the class name Employee
Attempts:
2 left
💡 Hint
Check how Django handles self-referencing ForeignKey declarations.
🧠 Conceptual
expert
3:00remaining
What is the main difference between symmetrical=True and symmetrical=False in self-referencing ManyToManyFields?
In Django, when defining a self-referencing ManyToManyField, what does setting symmetrical=True versus symmetrical=False change about the relationship?
Asymmetrical=True disables the relationship; symmetrical=False enables it
Bsymmetrical=True means if A is related to B, then B is automatically related to A; symmetrical=False means the relationship is one-way
Csymmetrical=True requires a through model; symmetrical=False does not
Dsymmetrical=True only works with ForeignKey; symmetrical=False only works with ManyToManyField
Attempts:
2 left
💡 Hint
Think about friendship versus following relationships.