I am trying to emulate an inheritance model using django's general relationship . So far this is what I came up with:
class Base(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() ... other stuff class Meta: unique_together = ("content_type", "object_id") class SubClass1(models.Model): ... properties for this class _base = generic.GenericRelation(Base) @property def base(self): return self._base.all()[0]
From what you can see here (at least I hope), SubClass1 should have a one-to-one relationship with Base , so I went through all the work of creating this _base field and then covering it with the Base property. If this common relation automatically leads to cascading hits, which is what I want (and I have a post-delete signal that will connect the delete from a different direction).
There can be any number of subclasses from Base , and what makes me feel sad is to copy and paste _base and its coverage property. I also have a user object manager that comes with this, and potentially other things that essentially should behave the same in every Base subclass.
Is there a good way to encapsulate this common functionality, so I don't need to copy and paste it for each subclass?
source share