python - 使用单个数据库表的 Django 模型继承 - 从父类(super class)访问子类的问题

标签 python django django-models single-table-inheritance django-inheritance

我正在使用 Oracle 开发 Django 应用程序,不允许修改数据库架构。 我有一个表,其中存在所有Thesis,它可以分为两个不相交的集合:PhdThesisBscMscThesis。我还有 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?我该怎么做才能得到这样的行为?

最佳答案

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()

关于python - 使用单个数据库表的 Django 模型继承 - 从父类(super class)访问子类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760207/

相关文章:

python - 在 python pandas 数据帧中添加时间序列强度的廉价方法

python - 如何在Azure VM上安装Python和Python包?

django - Django 应用程序的登录用户的负载平衡(或 http 代理)?

python - Dockerized Django 无法连接到 MySQL

python - Django CharField 在不应该的时候接受空值

python - 我如何跟踪有多少用户访问我的网站

python - 如何在python中读取大的tsv文件并将其转换为csv

django - 覆盖 Django 管理模板时如何访问模型数据?

python - 允许将 %d.%m.%Y 格式字符串传递给模型的 DateField 的最佳方法?

django - 如何使用 django 表单/模型来表示字段之间的选择?