diff --git a/CHANGELOG.md b/CHANGELOG.md index 7421fe3..cbfdf0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ #### Fixes +- fix: ```AUDITLOG_STORE_JSON_CHANGES=True``` was not respected during updates and deletions. + ## 3.2.0 (2025-06-26) #### Improvements diff --git a/auditlog/diff.py b/auditlog/diff.py index ba61e28..502f0a8 100644 --- a/auditlog/diff.py +++ b/auditlog/diff.py @@ -184,6 +184,8 @@ def model_instance_diff( :type new: Model :param fields_to_check: An iterable of the field names to restrict the diff to, while ignoring the rest of the model's fields. This is used to pass the `update_fields` kwarg from the model's `save` method. + :param use_json_for_changes: whether or not to use a JSON for changes + (see settings.AUDITLOG_STORE_JSON_CHANGES) :type fields_to_check: Iterable :return: A dictionary with the names of the changed fields as keys and a two tuple of the old and new field values as value. diff --git a/auditlog/receivers.py b/auditlog/receivers.py index acc02e8..525675a 100644 --- a/auditlog/receivers.py +++ b/auditlog/receivers.py @@ -64,6 +64,7 @@ def log_update(sender, instance, **kwargs): diff_old=old, diff_new=instance, fields_to_check=update_fields, + use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES, ) @@ -81,6 +82,7 @@ def log_delete(sender, instance, **kwargs): sender=sender, diff_old=instance, diff_new=None, + use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES, ) @@ -98,6 +100,7 @@ def log_access(sender, instance, **kwargs): diff_old=None, diff_new=None, force_log=True, + use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES, ) @@ -152,6 +155,7 @@ def _create_log_entry( changes=changes, log_entry=log_entry, log_created=log_entry is not None, + use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES, ) if error: raise error diff --git a/auditlog_tests/test_use_json_for_changes.py b/auditlog_tests/test_use_json_for_changes.py index b9b3720..844f700 100644 --- a/auditlog_tests/test_use_json_for_changes.py +++ b/auditlog_tests/test_use_json_for_changes.py @@ -1,6 +1,7 @@ from django.test import TestCase, override_settings from test_app.models import JSONModel, RelatedModel, SimpleModel +from auditlog.models import LogEntry from auditlog.registry import AuditlogModelRegistry @@ -113,3 +114,36 @@ class JSONForChangesTest(TestCase): field_one_to_one_changes = changes_dict["one_to_one"] self.assertEqual(field_one_to_one_changes, [None, simple.id]) + + @override_settings(AUDITLOG_STORE_JSON_CHANGES=True) + def test_use_json_for_changes_update(self): + self.test_auditlog.register_from_settings() + + simple = SimpleModel(text="original") + simple.save() + simple.text = "new" + simple.save() + + changes_dict = simple.history.latest().changes_dict + + text_changes = changes_dict["text"] + self.assertEqual(text_changes, ["original", "new"]) + + @override_settings(AUDITLOG_STORE_JSON_CHANGES=True) + def test_use_json_for_changes_delete(self): + self.test_auditlog.register_from_settings() + + simple = SimpleModel() + simple.save() + simple.delete() + + history = LogEntry.objects.all() + + self.assertEqual(history.count(), 1, '"DELETE" record is always retained') + + changes_dict = history.first().changes_dict + + self.assertTrue( + all(v[1] is None for k, v in changes_dict.items()), + 'all values in the changes dict should None, not "None"', + )