Django self-referential foreign key

前端 未结 4 792
渐次进展
渐次进展 2020-11-28 05:44

I\'m kind of new to webapps and database stuff in general so this might be a dumb question. I want to make a model (\"CategoryModel\") with a field that points to the primar

4条回答
  •  甜味超标
    2020-11-28 06:30

    You can use the string 'self' to indicate a self-reference.

    class CategoryModel(models.Model):
        parent = models.ForeignKey('self')
    

    https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

提交回复
热议问题