Django self-referential foreign key

前端 未结 4 783
渐次进展
渐次进展 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:40

    You also to sett null=True and blank=True

    class CategoryModel(models.Model):
        parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True)
    

    null=True, to allow in database
    blank=True, to allow in form validation

提交回复
热议问题