Python 用抽象类建立多对多关系模型

Python 用抽象类建立多对多关系模型,python,django,django-models,Python,Django,Django Models,我有以下型号。py class Institution(models.Model): name = models.CharField(_('Name'), max_length=150, db_index=True) slug = models.SlugField(_('Domain name'), unique=True) class Company(Institution): type = models.PositiveSmallIntegerField() c

我有以下型号。py

class Institution(models.Model):
    name = models.CharField(_('Name'), max_length=150, db_index=True)
    slug = models.SlugField(_('Domain name'), unique=True)

class Company(Institution):
    type = models.PositiveSmallIntegerField()


class HC(Institution):
    type = models.PositiveSmallIntegerField()
    bed_count = models.CharField(max_length=5, blank=True, null=True)
我有从机构生成的模型,我希望遵循概要模型中的机构

class Profile(models.Model):
    user = models.OneToOneField(User)
    about = models.TextField(_('About'), blank=True, null=True)
    following_profile = models.ManyToManyField('self', blank=True, null=True)
    following_institution = models.ManyToManyField(Institution, blank=True, null=True)
    following_tag = models.ManyToManyField(Tag, blank=True, null=True)

我想与所有继承机构的模型建立M2M关系。有没有办法用泛型关系做到这一点

听起来您已经准备好进行多态性:

否则,您必须单独添加它们,因此您的机构模型将如下所示:

class Institution(models.Model):
    name = models.CharField(_('Name'), max_length=150, db_index=True)
    slug = models.SlugField(_('Domain name'), unique=True)

    class Meta:
        abstract = True
following_company = models.ManyToManyField(Company, blank=True, null=True)
following_hc = models.ManyToManyField(Institution, blank=True, null=True)
许多人基本上是这样的:

class Institution(models.Model):
    name = models.CharField(_('Name'), max_length=150, db_index=True)
    slug = models.SlugField(_('Domain name'), unique=True)

    class Meta:
        abstract = True
following_company = models.ManyToManyField(Company, blank=True, null=True)
following_hc = models.ManyToManyField(Institution, blank=True, null=True)