Skip to content

Commit 899e69b

Browse files
authored
Merge branch 'master' into 422-add-history-extra-fields
2 parents 4f34cf8 + a6fddd6 commit 899e69b

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unreleased
66
- Add `custom_model_name` parameter to the constructor of `HistoricalRecords` (gh-451)
77
- Fix header on history pages when custom site_header is used (gh-448)
88
- Modify `pre_create_historircal_record` to pass `history_instance` for ease of customization (gh-421)
9+
- Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341)
910

1011
2.5.1 (2018-10-19)
1112
------------------

simple_history/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import importlib
55
import threading
66
import uuid
7+
import warnings
78

89
from django.apps import apps
910
from django.conf import settings
@@ -21,8 +22,8 @@
2122
from . import exceptions
2223
from .manager import HistoryDescriptor
2324
from .signals import (
24-
pre_create_historical_record,
2525
post_create_historical_record,
26+
pre_create_historical_record
2627
)
2728

2829
registered_models = {}
@@ -71,6 +72,11 @@ def contribute_to_class(self, cls, name):
7172
models.signals.class_prepared.connect(self.finalize, weak=False)
7273
self.add_extra_methods(cls)
7374

75+
if cls._meta.abstract and not self.inherit:
76+
msg = "HistoricalRecords added to abstract model ({}) without " \
77+
"inherit=True".format(self.cls.__name__)
78+
warnings.warn(msg, UserWarning)
79+
7480
def add_extra_methods(self, cls):
7581
def save_without_historical_record(self, *args, **kwargs):
7682
"""

simple_history/tests/tests/test_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,22 @@ def test_signal_is_able_to_retrieve_request_from_thread(self):
11281128

11291129
poll_history = polls[0].history.first()
11301130
self.assertEqual('127.0.0.1', poll_history.ip_address)
1131+
1132+
1133+
class WarningOnAbstractModelWithInheritFalseTest(TestCase):
1134+
def test_warning_on_abstract_model_with_inherit_false(self):
1135+
1136+
with warnings.catch_warnings(record=True) as w:
1137+
class AbstractModelWithInheritFalse(models.Model):
1138+
string = models.CharField()
1139+
history = HistoricalRecords()
1140+
1141+
class Meta:
1142+
abstract = True
1143+
1144+
self.assertEqual(len(w), 1)
1145+
self.assertTrue(issubclass(w[0].category, UserWarning))
1146+
self.assertEqual(str(w[0].message),
1147+
'HistoricalRecords added to abstract model '
1148+
'(AbstractModelWithInheritFalse) without '
1149+
'inherit=True')

0 commit comments

Comments
 (0)