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

2019-04-19 22:31发布

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条回答
趁早两清
2楼-- · 2019-04-19 23:24

I think this is what you're looking for:

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

See the docs for symmetrical.

查看更多
登录 后发表回答