Files
thrillwiki_django_no_react/history_tracking/mixins.py
2024-11-03 20:21:39 +00:00

24 lines
707 B
Python

# history_tracking/mixins.py
class HistoricalChangeMixin:
@property
def diff_against_previous(self):
prev_record = self.prev_record
if not prev_record:
return {}
changes = {}
for field in self.__dict__:
if field not in [
"history_date",
"history_id",
"history_type",
"history_user_id",
] and not field.startswith("_"):
old_value = getattr(prev_record, field)
new_value = getattr(self, field)
if old_value != new_value:
changes[field] = {"old": old_value, "new": new_value}
return changes