Skip to content

Commit 7755c1e

Browse files
committed
Documented overriding create_historical_record()
...as a more fine-grained alternative to the other ways of disabling historical record creation.
1 parent 8ea956c commit 7755c1e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

docs/disabling_history.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ See some examples below:
4343
Poll.objects.create(question="ignore this")
4444
Poll.objects.create(question="what's up?")
4545
46+
Overriding ``create_historical_record()``
47+
-----------------------------------------
48+
49+
For even more fine-grained control, you can subclass ``HistoricalRecords`` and override
50+
its ``create_historical_record()`` method, for example like this:
51+
52+
.. code-block:: python
53+
54+
class CustomHistoricalRecords(HistoricalRecords):
55+
def create_historical_record(
56+
self, instance: models.Model, history_type: str, *args, **kwargs
57+
) -> None:
58+
# Don't create records for "ignore" polls that are being deleted
59+
if "ignore" in poll.question and history_type == "-":
60+
return
61+
62+
super().create_historical_record(instance, history_type, *args, **kwargs)
63+
64+
65+
class Poll(models.Model):
66+
# ...
67+
history = CustomHistoricalRecords()
68+
4669
The ``SIMPLE_HISTORY_ENABLED`` setting
4770
--------------------------------------
4871

simple_history/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,9 @@ def create_historical_record_m2ms(self, history_instance, instance):
768768
field=field,
769769
)
770770

771-
def create_historical_record(self, instance, history_type, using=None):
771+
def create_historical_record(
772+
self, instance: models.Model, history_type: str, using: str = None
773+
) -> None:
772774
using = using if self.use_base_model_db else None
773775
history_date = getattr(instance, "_history_date", timezone.now())
774776
history_user = self.get_history_user(instance)

0 commit comments

Comments
 (0)