抽象实体之间的抽象关系

时间:2018-10-20 08:17:19

标签: django django-models

我想在以下模型类中添加一些抽象:

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)

并使PersonGroup继承EntityMembership的形式,PartnershipAcquaintanceshipRelation继承。 / p>

但是如何将子类的属性“链接”到父类?有没有更标准的方法来实现这一目标?

0 个答案:

没有答案