gpt4 book ai didi

Django 模型 : One to one relationship between objects of the same model class

转载 作者:行者123 更新时间:2023-12-03 16:29:41 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




8年前关闭。




Possible Duplicate:
Can I have a Django model that has a foreign key reference to itself?



我想在我的 Django 应用程序中实现一个简单的文件夹文件结构。所以我有一个存储文件夹的模型,但我也想存储这个文件夹和父文件夹之间的关系。模型的简化版本如下所示:
class mFolder(models.Model):
name = models.CharField(max_length=50)
parentFolder = models.ForeignKey(mFolder, unique=False, related_name="childrenFolders")

然而这是不可能的,因为 mFolder 尚未声明。

这个问题有什么简单的解决方案吗?

提前感谢您的帮助。

最佳答案

应该是“自我”:

parentFolder = models.ForeignKey('self', unique=False, related_name="childrenFolders")

关于Django 模型 : One to one relationship between objects of the same model class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12726897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com