Answer the question
In order to leave comments, you need to log in
How to create a parent-child relationship in a model?
I have a Category model
class Categorie(models.Model):
name = models.CharField('categorie_name', max_length=50)
parent = models.OneToOneField(Categorie, on_delete=models.CASCADE, blank=True)
child = models.ManyToManyField(Categorie, blank=True)
Answer the question
In order to leave comments, you need to log in
parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL, related_name='child')
Object.child.all()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question