我想在以下模型类中添加一些抽象:
class Person(models.Model):
groups = models.ManyToManyField('Group', through='Membership')
acquaintances = models.ManyToManyField('Group', through='Acquaintanceship')
class Group(models.Model):
members = models.ManyToManyField('Person', through='Membership')
partners = models.ManyToManyField('Person', through='Partnership')
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
class Partnership(models.Model):
group1 = models.ForeignKey(Group, on_delete=models.CASCADE)
group2 = models.ForeignKey(Group, on_delete=models.CASCADE)
class Acquaintanceship(models.Model):
person1 = models.ForeignKey(Person, on_delete=models.CASCADE)
person2 = models.ForeignKey(Person, on_delete=models.CASCADE)
我可以通过创建抽象类来实现:
class Entity(models.Model):
pass
class Relation(models.Model):
entity1 = models.ForeignKey(Person, on_delete=models.CASCADE)
entity2 = models.ForeignKey(Person, on_delete=models.CASCADE)
并使Person
和Group
继承Entity
和Membership
的形式,Partnership
和Acquaintanceship
从Relation
继承。 / p>
但是如何将子类的属性“链接”到父类?有没有更标准的方法来实现这一目标?