Complete the code to define a self-referencing ForeignKey in a Django model.
class Category(models.Model): name = models.CharField(max_length=100) parent = models.ForeignKey('[1]', on_delete=models.CASCADE, null=True, blank=True)
In Django, to create a self-referencing ForeignKey, you use the string 'self' to refer to the same model class.
Complete the code to allow a Category to have many children categories.
class Category(models.Model): name = models.CharField(max_length=100) parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name=[1])
The related_name defines the reverse relation name. 'children' is a common choice for self-referencing parent-child relations.
Fix the error in the model field to correctly allow null parent categories.
parent = models.ForeignKey('self', on_delete=models.CASCADE, [1]=True, blank=True, related_name='children')
The correct argument to allow a ForeignKey to be empty in the database is null=True.
Fill both blanks to create a self-referencing ManyToManyField with a symmetrical relationship.
class Person(models.Model): name = models.CharField(max_length=100) friends = models.[1]('self', [2]=True, blank=True)
A self-referencing ManyToManyField uses symmetrical=True to indicate the relationship is mutual, like friendship.
Fill all three blanks to define a self-referencing ManyToManyField with an asymmetrical relationship and a custom reverse name.
class Employee(models.Model): name = models.CharField(max_length=100) mentors = models.[1]('self', symmetrical=[2], [3]='mentees', blank=True)
For a mentorship relation, the ManyToManyField is asymmetrical (symmetrical=False) and uses related_name='mentees' for reverse access.