I am actually stuck in a problem where I need to exclude HistoricalRecords (django-simple-history) from a child class without making any changes in the parent class.
Here is my code looks like
from simple_history.models import HistoricalRecords
class BaseModel(models.Model):
id = models.UUIDField(...some args...)
created_at = models.DateTimeField(...some args...)
created_by = models.ForeignKey(...some args...)
history = HistoricalRecords(inherit=True)
class Meta:
abstract = True
default_permissions = Constants.STANDARD_ACTIONS
def save(self, suppress_modified=False, from_publish=False, **kwargs):
super(BaseModel, self).save(**kwargs)
class CommonWithExtraFieldsWithoutVersioning(BaseModel, CommonWithoutVersioning, models.Model):
class Meta(CommonWithoutVersioning.Meta):
abstract = True
class Manager(CommonWithExtraFieldsWithoutVersioning):
from_branch = models.CharField(...some args...)
to_branch = models.CharField(...some args...)
from_version = models.IntegerField(...some args...)
to_version = models.IntegerField(...some args...)
history = None # this is also not working for me
def __init__(self, *args, **kwargs): # this I have tried but not working
super(DSLJourneyMergeHistory, self).__init__(*args, **kwargs)
self.clean_fields('history')
But when I try to makemigration
it is creating HistoricalRecords for the manager as well and I want HistoricalRecords
should not be maintained for the class manager
. How do I exclude this as BaseModel and CommonWithExtraFields Models are used across the application and I cannot make any modification in that model
The reason this is happening is because you have inherit=True
in your BaseModel. When this flag is set to True, any inherited child class will also have simple_history set up. If you'd like to turn off this inheritance, you need to update inherit=False
in BaseModel
. You can read more about this behavior in the simple-history docs.