Last active
December 16, 2015 19:09
-
-
Save eezis/5482782 to your computer and use it in GitHub Desktop.
No FK on abstract base class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a contrived example that illustrates my problem. I want to subclass a base class (PersonBase) | |
into several different descendant classes. Each person type can have multiple emails and phone numbers | |
The email and phone class should have a 1..n relationship to all the different people subclasses. | |
I thought could structure this as follows, but the FK cannot be created on the abstract base class. What | |
is the best practice for solving this situation . . . instantiate that base model instead of making it | |
abstract? Or try to implement GenericForeignKeys, or do they introduce more problems than they solve? | |
Here's the problem in pseudocode . . . | |
class PersonBase(models.Model): | |
first = models.CharField(max_length=80) | |
middle = models.CharField(blank=True, max_length=80) | |
last = models.CharField(max_length=80) | |
suffix = models.CharField(blank=True, max_length=10) | |
class Meta: | |
abstract = True | |
class Family(PersonBase): | |
relationship = field.choice(father, son, mother, daughter . . .) | |
class Advisor(PersonBase): | |
relationship = field.choice(professor, mentor, tutor. . . ) | |
class Email(models.Model): | |
person = ForeignKey(PersonBase) <== CAN'T DO THIS, THE BASE IS NEVER CREATED IN THE DB | |
emailtype = field.choice(personal, work, shared) | |
email_address = field.emailfield() | |
class Phone(models.Model): | |
person = models.ForeignKey(PersonBase) <== CAN'T DO THIS, THE BASE IS NEVER CREATED IN THE DB | |
phonetype = field.choice(cell, work, home) | |
number = field.phonefield() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment