Django model inheritance using single DB table - problems accessing subclass from superclass
I am developing a Django application using Oracle with no modifications to DB schema allowed.
I have one table in which all Thesis
exist, which can be seperated in two disjoint sets: PhdThesis
and BscMscThesis
. I have also Review
model, which links to Thesis
DB table and doesn't care whether it is PhdThesis
or BscMscThesis
, so I would like to keep Thesis
as abstract = False
class.
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")
The problem I encountered is:
>>> 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()
and BscMscThesis.objects.all()
return two disjoint sets as intended.
Why does not Thesis.objects.all()[0].phdthesis
return None or DoesNotExist in above example? What can I do to get such behaviour?
It seems there is no easy way for Django to do this. I ended up implementing methods like is_phd()
and is_bsc_msc()
and using them like this:
def get_absolute_url(self):
if self.is_phd():
return self.phdthesis.get_absolute_url()
else:
return self.bscmscthesis.get_absolute_url()
精彩评论