django-simple-history icon indicating copy to clipboard operation
django-simple-history copied to clipboard

Set HistoricalRecord base class globally

Open jws opened this issue 1 year ago • 1 comments

Problem Statement Is it possible to set the base class(s) for HistoricalRecord globally?

  • So platform wide fields can be set in one place instead of on every instantiation of HistoricalRecord (as described here)

Describe the solution you'd like Ability to set base classes for HistoricalRecord globally.

Describe alternatives you've considered

  • Patching logic directly into HistoricalRecord.
  • Overriding HistoricalRecord and using the override everywhere instead + linter to prevent accidental bypass of the overridden class.

jws avatar Jul 26 '23 18:07 jws

Not sure what your looking for. But, I hope this helps

Create a Model base class and add the history attribute

class CommonBaseModel(models.Model):
    history = HistoricalRecords(
        inherit=True,
        bases=[RoutableModel, ]
    )
    
    class Meta:
         abstract = True
    

Then when you create your models you'll have the settings you require in the parent class

class SomeModel(CommonBaseModel):
     question = models.CharField(max_length=200)

NoelJames avatar Sep 07 '23 23:09 NoelJames