Skip to content

Commit 7b12aef

Browse files
committed
Allow historical tracking to be set on abstract bases
1 parent 56279dc commit 7b12aef

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

simple_history/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
4949
def contribute_to_class(self, cls, name):
5050
self.manager_name = name
5151
self.module = cls.__module__
52-
models.signals.class_prepared.connect(self.finalize, sender=cls)
52+
self.cls = cls
53+
models.signals.class_prepared.connect(self.finalize, weak=False)
5354
self.add_extra_methods(cls)
5455

5556
def add_extra_methods(self, cls):
@@ -69,6 +70,11 @@ def save_without_historical_record(self, *args, **kwargs):
6970
save_without_historical_record)
7071

7172
def finalize(self, sender, **kwargs):
73+
try:
74+
if not issubclass(sender, self.cls):
75+
return
76+
except AttributeError: # called via `register`
77+
pass
7278
history_model = self.create_history_model(sender)
7379
module = importlib.import_module(self.module)
7480
setattr(module, history_model.__name__, history_model)

simple_history/tests/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,14 @@ class ContactRegister(models.Model):
277277
email = models.EmailField(max_length=255, unique=True)
278278

279279
register(ContactRegister, table_name='contacts_register_history')
280+
281+
282+
class TrackedAbstractBase(models.Model):
283+
history = HistoricalRecords()
284+
285+
class Meta:
286+
abstract = True
287+
288+
289+
class Tracked(TrackedAbstractBase):
290+
pass

simple_history/tests/tests/test_models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
2020
HistoricalState, HistoricalCustomFKError, Series, SeriesWork, PollInfo,
2121
UserAccessorDefault, UserAccessorOverride, Employee, Country, Province,
22-
City, Contact, ContactRegister
22+
City, Contact, ContactRegister, Tracked
2323
)
2424
from ..external.models import ExternalModel2, ExternalModel4
2525

@@ -783,3 +783,9 @@ def test_custom_table_name_from_register(self):
783783
self.get_table_name(ContactRegister.history),
784784
'contacts_register_history',
785785
)
786+
787+
788+
class TestAbstractHistoricalBaseModel(TestCase):
789+
790+
def test_historical_abstract(self):
791+
Tracked.history.all()

0 commit comments

Comments
 (0)