[Fixed]-Storing related model references in Django?

1๐Ÿ‘

โœ…

I think the most straightforward way would be to store the value as a class attribute:

class HistoricalFoo(models.Model):
    ...

class Foo(HistoryTrackedModel):
    history_model = HistoricalFoo
    ....

class HistoryTrackedModel(models.Model):
    def save(self):
        ...
        h = self.history_model(...)

An alternative would be to generate the historical model names programmatically:

class HistoryTrackedModel(models.Model):
    def save(self):
        ...
        history_model = globals()["Historical" + self.__class__.__name__]
        h = history_model(...)

Leave a comment