Skip to content

Commit

Permalink
Documented overriding create_historical_record()
Browse files Browse the repository at this point in the history
...as a more fine-grained alternative to the other ways of disabling
historical record creation.
  • Loading branch information
ddabble committed Sep 10, 2024
1 parent 8ea956c commit 7755c1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 23 additions & 0 deletions docs/disabling_history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ See some examples below:
Poll.objects.create(question="ignore this")
Poll.objects.create(question="what's up?")
Overriding ``create_historical_record()``
-----------------------------------------

For even more fine-grained control, you can subclass ``HistoricalRecords`` and override
its ``create_historical_record()`` method, for example like this:

.. code-block:: python
class CustomHistoricalRecords(HistoricalRecords):
def create_historical_record(
self, instance: models.Model, history_type: str, *args, **kwargs
) -> None:
# Don't create records for "ignore" polls that are being deleted
if "ignore" in poll.question and history_type == "-":
return
super().create_historical_record(instance, history_type, *args, **kwargs)
class Poll(models.Model):
# ...
history = CustomHistoricalRecords()
The ``SIMPLE_HISTORY_ENABLED`` setting
--------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,9 @@ def create_historical_record_m2ms(self, history_instance, instance):
field=field,
)

def create_historical_record(self, instance, history_type, using=None):
def create_historical_record(
self, instance: models.Model, history_type: str, using: str = None
) -> None:
using = using if self.use_base_model_db else None
history_date = getattr(instance, "_history_date", timezone.now())
history_user = self.get_history_user(instance)
Expand Down

0 comments on commit 7755c1e

Please sign in to comment.