I have a model:
class Industry(models.Model):
name = models.CharField(max_length=60, unique=True, null=False, blank=False)
I want it to have a sub_industries field that will hold all of its sub industries, which should be instances of Industry model.
I cannot be ManyToMany because each industry has only one parent industry.
Advertisement
Answer
You can add a ForeignKey to itself with:
class Industry(models.Model):
name = models.CharField(max_length=60, unique=True)
parent_industry = models.ForeignKey(
'self',
null=True,
related_name='sub_industries'
)You can use NULL/None to express that an industry has no parent.