Created
April 23, 2013 11:37
-
-
Save orymate/5442879 to your computer and use it in GitHub Desktop.
Abstract base class model that provides self-updating discriminators for fast multi-table inheritance.
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
from django.db import models | |
class DiscriminatedModel(models.Model): | |
""" | |
An abstract base class model that provides self-updating discriminators for | |
fast multi-table inheritance. | |
""" | |
discriminator = models.CharField(max_length=255) | |
def get_subclass(self): | |
""" | |
Get concrete subclass throught reverse of implicit OneToOne link. | |
""" | |
try: | |
return getattr(self, self.discriminator) | |
except: | |
return self | |
def save(self, *args, **kwargs): | |
self.discriminator = self.__class__.__name__.lower() | |
super(DiscriminatedModel, self).save(*args, **kwargs) | |
class Meta: | |
abstract = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment