Django模型通过单一数据库表继承 - 从超类访问子类的问题

1 投票
1 回答
772 浏览
提问于 2025-04-16 21:54

我正在开发一个使用Oracle的Django应用程序,数据库的结构不允许修改。

我有一个表格,里面存放着所有的Thesis(论文),这些论文可以分成两个不重叠的类别:PhdThesis(博士论文)和BscMscThesis(学士和硕士论文)。我还有一个Review(评论)模型,它与Thesis表格相连,不管是PhdThesis还是BscMscThesis,它都不在乎,所以我想把Thesis设置为abstract = False的类。

class Thesis(models.Model):
    # Primary key  has to be specified explicite here for inheritance to work?
    id = models.DecimalField(db_column="ID", max_digits=10, decimal_places=0, primary_key=True)
    class Meta:
        db_table = "DZ_PRACE_CERT"
        managed = False

class Person(models.Model):
    class Meta:
        db_table = "MV_OSOBY"
        managed = False


class BscMscThesisManager(models.Manager):
    def get_query_set(self):
        return super(BscMscThesisManager, self).get_query_set().filter(personbscmscdiploma__isnull=False)

class BscMscThesis(Thesis):
    # needed for inheritance?
    thesis = models.OneToOneField(Thesis, db_column="ID", primary_key=True, parent_link=True)
    authors = models.ManyToManyField(Person, through="PersonBscMscDiploma", related_name='author_of_bsc_msc_theses')
    objects = BscMscThesisManager()

    class Meta:
        db_table = "DZ_PRACE_CERT"
        managed = False

class PersonBscMscDiploma(models.Model):
    bsc_msc_thesis = models.ForeignKey(BscMscThesis, db_column="PRC_CERT_ID")


class PhdThesisManager(models.Manager):
    def get_query_set(self):
        return super(PhdThesisManager, self).get_query_set().filter(personphddiploma__isnull=False)

class PhdThesis(Thesis):
    # needed for inheritance?
    thesis = models.OneToOneField(Thesis, db_column="ID", primary_key=True, parent_link=True)
    authors = models.ManyToManyField(Person, through="PersonPhdDiploma", related_name='author_of_phd_theses')
    objects = PhdThesisManager()

    class Meta:
        db_table = "DZ_PRACE_CERT"
        managed = False

class PersonPhdDiploma(models.Model):
    phd_thesis = models.ForeignKey(PhdThesis, db_column="PRC_CERT_ID")

我遇到的问题是:

>>> Thesis.objects.all()[0].phdthesis
<PhdThesis: Uniwersytecki System Obsługi Studiów. Parametryzowane filtry>
>>> Thesis.objects.all()[0].bscmscthesis
<BscMscThesis: Uniwersytecki System Obsługi Studiów. Parametryzowane filtry>
>>> Thesis.objects.all()[0].phdthesis.authors.all()
[]
>>> Thesis.objects.all()[0].bscmscthesis.authors.all()
[<Person: Jan1912 Kowalski1912>]
>>> Thesis.objects.all()[0].id
Decimal('903')
>>> BscMscThesis.objects.get(id=903)
<BscMscThesis: Uniwersytecki System Obsługi Studiów. Parametryzowane filtry>
>>> PhdThesis.objects.get(id=903)
DoesNotExist: PhdThesis matching query does not exist.

PhdThesis.objects.all()BscMscThesis.objects.all()返回了两个不重叠的集合,这正是我想要的。

但是,为什么Thesis.objects.all()[0].phdthesis没有返回None或者DoesNotExist呢?我该怎么做才能让它有这样的表现呢?

1 个回答

1

看起来Django没有简单的方法来做到这一点。我最后实现了一些方法,比如 is_phd()is_bsc_msc(),然后像这样使用它们:

def get_absolute_url(self):
    if self.is_phd():
        return self.phdthesis.get_absolute_url()
    else:
        return self.bscmscthesis.get_absolute_url()

撰写回答