I have a model Person
which stores all data about people. I also have a Client
model which extends Person. I have another extending model OtherPerson
which also extends the Person
model. I want to create a Client pointing to a Person
, and ALSO create an OtherPerson
record which points to that Person
. Basically, I want one Person
object to be viewed as a Client
and and an OtherPerson
, depending on the current view. Is this possible with Django's ORM, or do I need to somehow write a Raw query to create this scenario. I am quite certain it is possible from the database side, because both child classes would just point to the parent Person class with their person_ptr_id field.
Simply put, if I create a Client
(and thus a Person
), can I also create an OtherPerson
object using the base Person
from the Client
. That way I can view them as a Client
OR as an OtherPerson
, and saving one will affect the Person
fields of each?
"Horizontal" Polymorphism?
Here is a cut down version of my models in case that clarifies:
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>