我可以有一个Django模型,它有一个外键引用自身吗? [英] Can I have a Django model that has a foreign key reference to itself?
本文介绍了我可以有一个Django模型,它有一个外键引用自身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我该怎么做?
class Example(models.Model):
parent_example = models.ForeignKey(Example)
我想有一个模型有一个外键引用自己。当我尝试创建它,我得到一个django验证错误,示例还没有定义。
I want to have a model have a foreign key reference to itself. When I try to create this I get a django validation error that Example is not yet defined.
推荐答案
你应该使用
You should use
models.ForeignKey('self')
如 here 所述。
这篇关于我可以有一个Django模型,它有一个外键引用自身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文