Challenge - 5 Problems
Self-Referencing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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'}"
Attempts:
2 left
💡 Hint
Check how the __str__ method handles the parent attribute when it is None.
✗ Incorrect
The __str__ method returns the category name and the parent's name if it exists. Since parent is None, it prints 'None'.
❓ state_output
intermediate2: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
Attempts:
2 left
💡 Hint
Look at how the parent is assigned when creating the child node.
✗ Incorrect
The child node's parent is set to the root node, so accessing child.parent.name returns 'Root'.
📝 Syntax
advanced2: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 = ???
Attempts:
2 left
💡 Hint
Friends usually have a symmetrical relationship.
✗ Incorrect
For friends, the relationship is symmetrical, so symmetrical=True is correct. Using 'self' as the model name is required for self-referencing.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check how Django handles self-referencing ForeignKey declarations.
✗ Incorrect
When referencing the same model inside its definition, Django requires the model name as a string 'self' to avoid errors.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about friendship versus following relationships.
✗ Incorrect
symmetrical=True means the relationship is mutual (like friends). symmetrical=False means the relationship can be one-directional (like followers).