model self-dependency (one-to-many field) implementation

断了今生、忘了曾经 提交于 2019-12-10 01:25:19

问题


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


回答1:


I think this is what you're looking for:

dependencies = models.ManyToManyField("self", symmetrical=False)

See the docs for symmetrical.



来源:https://stackoverflow.com/questions/16613013/model-self-dependency-one-to-many-field-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!