我有一个模型Person
它存储有关人的所有数据。我也有一个Client
扩展 Person 的模型。我有另一个扩展模型OtherPerson
这也扩展了Person
模型。我想创建一个指向Person
,并且还创建一个OtherPerson
记录指向那个的Person
。基本上我想要一个Person
对象被视为Client
和一个OtherPerson
,取决于当前视图。这可以通过 Django 的 ORM 实现吗?或者我是否需要以某种方式编写原始查询来创建此场景。我非常确定从数据库方面来说这是可能的,因为两个子类都只会使用其 person_ptr_id 字段指向父 Person 类。
简而言之,如果我创建一个Client
(因此Person
),我还可以创建一个OtherPerson
使用底座的对象Person
来自Client
。这样我就可以将它们视为Client
或作为OtherPerson
,保存一个会影响Person
各自的领域?
“水平”多态性?
这是我的模型的简化版本,以防澄清:
class Person(models.Model):
"""
Any person in the system will have a standard set of details, fingerprint hit details, some clearances and items due, like TB Test.
"""
first_name = models.CharField(db_index=True, max_length=64, null=True, blank=True, help_text="First Name.")
middle_name = models.CharField(db_index=True, max_length=32, null=True, blank=True, help_text="Middle Name.")
last_name = models.CharField(db_index=True, max_length=64, null=True, blank=True, help_text="Last Name.")
alias = models.CharField(db_index=True, max_length=128, null=True, blank=True, help_text="Aliases.")
.
.
<some person methods like getPrintName, getAge, etc.>
class Client(Person):
date_of_first_contact = models.DateField(null=True, blank=True)
.
.
<some client methods>
class OtherPerson(Person):
active_date = models.DateField(null=True, blank=True)
termination_date = models.DateField(null=True, blank=True)
.
.
<some other person methods>