问题描述
我想实现一个具有自我依赖性的模型。说实例People_A可能依赖于People_B和People_C。我首先实现这个模型,有很多到很多键。
class People(models.Model):
dependency = models。 ManyToManyField('self',blank = True,null = True)
但结果是如果People_A依靠People_B将导致People_B依赖于People_A。这是我不想拥有的东西。
然后我用外键实现它。
class People model.Model):
dependency = models.ForeignKey('self',blank = True,null = True)
但这不行。如果People_A依赖于People_B,那么没有其他人可以依靠People_B。它将以最新的依赖关系覆盖旧的依赖关系。
任何线索都会感激
我认为这是你要找的:
dependencies = models.ManyToManyField(self ,对称= False)
请参阅用于对称。
I would like to implement a model with self-dependency. Say instance People_A may depend on People_B and People_C.I first implement this model with many to many key.
class People(models.Model):
dependency = models. ManyToManyField ('self', blank=True, null=True)
But the result is that if People_A depend on People_B will result in People_B depend also on People_A. That’s something I don’t want to have.
Then I implement it with foreign key.
class People(models.Model):
dependency = models.ForeignKey('self', blank=True, null=True)
But this doesn’t work also. If People_A depend on People_B, then no other People could depend on People_B. It will cover the old dependency with the latest dependency.
Any clue would be thankful
I think this is what you're looking for:
dependencies = models.ManyToManyField("self", symmetrical=False)
See the docs for symmetrical.
这篇关于模型自我依赖(一对多领域)实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!