【发布时间】:2013-08-08 15:31:47
【问题描述】:
我有一个模型Person,它存储有关人员的所有数据。我还有一个扩展 Person 的 Client 模型。我有另一个扩展模型OtherPerson,它也扩展了Person 模型。我想创建一个指向Person 的客户端,并且还创建一个指向Person 的OtherPerson 记录。基本上,我希望将一个Person 对象视为Client 和一个OtherPerson,具体取决于当前视图。这可能与 Django 的 ORM,还是我需要以某种方式编写一个原始查询来创建这个场景。我很确定这在数据库方面是可能的,因为两个子类都只会指向带有 person_ptr_id 字段的父 Person 类。
简单地说,如果我创建一个Client(因此是一个Person),我还可以使用来自Client 的基础Person 创建一个OtherPerson 对象。这样我可以将它们视为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>
【问题讨论】: