问题
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