Skip to content

Commit c010834

Browse files
amartisjeking3
authored andcommitted
Make change reason customizable with overriding.
Shortcoming of previous architecture: Recording reason for change was only through manually setting to each object or through bulk_history_create. So recording API request as reason automatically was hard to implement with this architecture. Solution: Getting change reason is now done through `HistoricalRecords` method `get_change_reason_for_object`. And thus this method can be overriden in subclass to define custom behavior.
1 parent d8dc4d2 commit c010834

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Authors
1515
- Aleksey Kladov
1616
- Alexander Anikeev
1717
- Amanda Ng (`AmandaCLNg <https://github.com/AmandaCLNg>`_)
18+
- Amartis Gladius (`Amartis <https://github.com/amartis>`_)
1819
- Ben Lawson (`blawson <https://github.com/blawson>`_)
1920
- Benjamin Mampaey (`bmampaey <https://github.com/bmampaey>`_)
2021
- `bradford281 <https://github.com/bradford281>`_

simple_history/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,18 @@ def post_delete(self, instance, using=None, **kwargs):
552552
else:
553553
self.create_historical_record(instance, "-", using=using)
554554

555+
def get_change_reason_for_object(self, instance, history_type, using):
556+
"""
557+
Get change reason for object.
558+
Customize this method to automatically fill change reason from context.
559+
"""
560+
return get_change_reason_from_object(instance)
561+
555562
def create_historical_record(self, instance, history_type, using=None):
556563
using = using if self.use_base_model_db else None
557564
history_date = getattr(instance, "_history_date", timezone.now())
558565
history_user = self.get_history_user(instance)
559-
history_change_reason = get_change_reason_from_object(instance)
566+
history_change_reason = self.get_change_reason_for_object(instance, history_type, using)
560567
manager = getattr(instance, self.manager_name)
561568

562569
attrs = {}

0 commit comments

Comments
 (0)