In Django, I cannot downcast (attributes are missing). Any trivial solution breaks the Open-Closed Principle. This Solution not: from django.db import models class ParentModel ( models . Model ): type = models.CharField( max_length = 255 , default = 'item' ) class Meta : abstract = True def save (self, *args, **kwargs): self .type = self .type_str super ().save(*args, **kwargs) @ staticmethod def register_subtype (clazz): the_type = clazz.type_str if not hasattr (ParentModel, 'subclasses' ): ParentModel.subclasses = {} ParentModel.subclasses[the_type] = clazz def downcast (self): if self .type not in ParentModel.subclasses: return self return ParentModel.subclasses[ self .type].objects.get( pk = self .pk) With the example Item: class Item ( ParentModel ): """Stuff you can purchase""" type_str = 'item' ...
"If you think without writing, you only think you're thinking." - Leslie Lamport | Contents are snapshots of thoughts; they change within minutes.