[Django]-Can I have a Django model that has a foreign key reference to itself?

122👍

You should use

models.ForeignKey('self')

as mentioned here.

👤ohnoes

21👍

Yes, just do this:

class Example(models.Model):
  parent_example = models.ForeignKey('self')

1👍

You can do this using quotes too:

class Example(models.Model):
    parent_example = models.ForeignKey('Example')

Leave a comment