【发布时间】:2015-08-10 22:05:57
【问题描述】:
我有一个抽象模型:
class Distributor(models.Model):
class Meta:
abstract = True
和 2 个继承它的模型:
class DistributorGroup(Distributor):
pass
class DistributorPerson(Distributor):
pass
我还有一个链接模型:
class Link(models.Model):
distributors_persons = models.ManyToManyField(
'people.DistributorPerson', blank=True, related_name='distributors_persons_of_links')
distributors_groups = models.ManyToManyField(
'groups.DistributorGroup', blank=True, related_name='distributors_groups_of_links')
链接可以与其中一个分销商有关系。我通过添加 2 m2m 并将 blank=True 设置为两者来完成此行为。
现在我意识到我需要一个through 模型来连接Distributors 和Link。但是由于through 模型不能将抽象模型作为外键,所以我不知道该怎么做。我是否需要为 DistributorPerson 和 DistributorGroup 创建 2 个单独的 through 模型,或者有一种方法可以使用 1 个 through 模型完成此操作。另外我不知道我在Link 模型中的 2 m2m 是否是完成我想要的行为的正确方法。
所以我想知道用抽象模型组织这些 m2m 模型的方法是什么。
【问题讨论】:
标签: python django django-models django-orm