From 816cf9ed88ce101a07dbc824975cc323b30e17eb Mon Sep 17 00:00:00 2001 From: Marco Marra Date: Mon, 12 Jan 2026 10:43:04 +0100 Subject: [PATCH 1/6] Add test case to test log entry changes_str property for m2m changes. --- auditlog_tests/tests.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index aba766e..5945bd5 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -494,6 +494,14 @@ class ManyRelatedModelTest(TestCase): }, ) + def test_changes_str(self): + self.obj.related.add(self.related) + log_entry = self.obj.history.first() + self.assertEqual( + log_entry.changes_str, + f"related: add {[smart_str(self.related)]}" + ) + def test_adding_existing_related_obj(self): self.obj.related.add(self.related) log_entry = self.obj.history.first() From 3701328c219b7d869cef30bebf4a655a409925d3 Mon Sep 17 00:00:00 2001 From: Marco Marra Date: Mon, 12 Jan 2026 11:04:15 +0100 Subject: [PATCH 2/6] Add support for m2m field changes and generic changes in AbstractLogEntry.changes_str property. --- auditlog/models.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/auditlog/models.py b/auditlog/models.py index edc5dd2..a7c94a2 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -1,7 +1,7 @@ import ast import contextlib import json -from collections.abc import Callable +from collections.abc import Callable, Sequence from copy import deepcopy from datetime import timezone from typing import Any @@ -427,21 +427,33 @@ class AbstractLogEntry(models.Model): not satisfying, please use :py:func:`LogEntry.changes_dict` and format the string yourself. :param colon: The string to place between the field name and the values. - :param arrow: The string to place between each old and new value. + :param arrow: The string to place between each old and new value (non-m2m field changes only). :param separator: The string to place between each field. :return: A readable string of the changes in this log entry. """ - substrings = [] - - for field, values in self.changes_dict.items(): - substring = "{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}".format( - field_name=field, - colon=colon, - old=values[0], - arrow=arrow, - new=values[1], - ) - substrings.append(substring) + if all(isinstance(value, Sequence) for value in self.changes_dict.values()): + substrings = [ + "{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}".format( + field_name=field, + colon=colon, + old=values[0], + arrow=arrow, + new=values[1], + ) + for field, values in self.changes_dict.items() + ] + elif all( + isinstance(value, dict) and value.get("type") == "m2m" + for value in self.changes_dict.values() + ): + substrings = [ + f"{field}{colon}{value_dict['operation']} {value_dict['objects']}" + for field, value_dict in self.changes_dict.items() + ] + else: + substrings = [ + f"{field}{colon}{value}" for field, value in self.changes_dict.items() + ] return separator.join(substrings) From dbe49fe49ee8be58495a0da177c784232da7cd52 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jan 2026 10:08:20 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- auditlog_tests/tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 5945bd5..d991bef 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -498,8 +498,7 @@ class ManyRelatedModelTest(TestCase): self.obj.related.add(self.related) log_entry = self.obj.history.first() self.assertEqual( - log_entry.changes_str, - f"related: add {[smart_str(self.related)]}" + log_entry.changes_str, f"related: add {[smart_str(self.related)]}" ) def test_adding_existing_related_obj(self): From ce58befd2747c302dcdd4633fbf59871c500784f Mon Sep 17 00:00:00 2001 From: Marco Marra Date: Mon, 12 Jan 2026 12:20:01 +0100 Subject: [PATCH 4/6] Add more test cases for changes_str. --- auditlog/models.py | 10 ++++------ auditlog_tests/tests.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/auditlog/models.py b/auditlog/models.py index a7c94a2..dd5158e 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -431,6 +431,8 @@ class AbstractLogEntry(models.Model): :param separator: The string to place between each field. :return: A readable string of the changes in this log entry. """ + substrings = [] + if all(isinstance(value, Sequence) for value in self.changes_dict.values()): substrings = [ "{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}".format( @@ -440,20 +442,16 @@ class AbstractLogEntry(models.Model): arrow=arrow, new=values[1], ) - for field, values in self.changes_dict.items() + for field, values in sorted(self.changes_dict.items()) ] elif all( isinstance(value, dict) and value.get("type") == "m2m" for value in self.changes_dict.values() ): substrings = [ - f"{field}{colon}{value_dict['operation']} {value_dict['objects']}" + f"{field}{colon}{value_dict['operation']} {sorted(value_dict['objects'])}" for field, value_dict in self.changes_dict.items() ] - else: - substrings = [ - f"{field}{colon}{value}" for field, value in self.changes_dict.items() - ] return separator.join(substrings) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index d991bef..2237e3a 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -131,6 +131,11 @@ class SimpleModelTest(TestCase): {"boolean": ["False", "True"]}, msg="The change is correctly logged", ) + self.assertEqual( + history.changes_str, + "boolean: False → True", + msg="Changes string is correct", + ) def test_update_specific_field_supplied_via_save_method(self): obj = self.obj @@ -149,6 +154,11 @@ class SimpleModelTest(TestCase): "when using the `update_fields`." ), ) + self.assertEqual( + obj.history.get(action=LogEntry.Action.UPDATE).changes_str, + "boolean: False → True", + msg="Changes string is correct", + ) def test_django_update_fields_edge_cases(self): """ @@ -179,6 +189,11 @@ class SimpleModelTest(TestCase): {"boolean": ["False", "True"], "integer": ["None", "1"]}, msg="The 2 fields changed are correctly logged", ) + self.assertEqual( + obj.history.get(action=LogEntry.Action.UPDATE).changes_str, + "boolean: False → True; integer: None → 1", + msg="Changes string is correct", + ) def test_delete(self): """Deletion is logged correctly.""" @@ -732,6 +747,11 @@ class SimpleIncludeModelTest(TestCase): {"label": ["Initial label", "New label"]}, msg="Only the label was logged, regardless of multiple entries in `update_fields`", ) + self.assertEqual( + obj.history.get(action=LogEntry.Action.UPDATE).changes_str, + "label: Initial label → New label", + msg="Changes string is correct", + ) def test_register_include_fields(self): sim = SimpleIncludeModel(label="Include model", text="Looong text") @@ -2068,6 +2088,11 @@ class JSONModelTest(TestCase): {"json": ["{}", '{"quantity": "1"}']}, msg="The change is correctly logged", ) + self.assertEqual( + history.changes_str, + "json: {} → {\"quantity\": \"1\"}", + msg="Changes string is correct", + ) def test_update_with_no_changes(self): """No changes are logged.""" @@ -2704,6 +2729,7 @@ class TestAccessLog(TestCase): ) self.assertIsNone(log_entry.changes) self.assertEqual(log_entry.changes_dict, {}) + self.assertEqual(log_entry.changes_str, "") class SignalTests(TestCase): @@ -3127,6 +3153,7 @@ class BaseManagerSettingTest(TestCase): } }, ) + self.assertEqual(log_entry.changes_str, f"m2m_related: add {[smart_str(obj_two)]}") class TestMaskStr(TestCase): From f356474eca0b116f7e1c22db858b176658f0bad5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jan 2026 11:20:53 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- auditlog_tests/tests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 2237e3a..6764195 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -2090,7 +2090,7 @@ class JSONModelTest(TestCase): ) self.assertEqual( history.changes_str, - "json: {} → {\"quantity\": \"1\"}", + 'json: {} → {"quantity": "1"}', msg="Changes string is correct", ) @@ -3153,7 +3153,9 @@ class BaseManagerSettingTest(TestCase): } }, ) - self.assertEqual(log_entry.changes_str, f"m2m_related: add {[smart_str(obj_two)]}") + self.assertEqual( + log_entry.changes_str, f"m2m_related: add {[smart_str(obj_two)]}" + ) class TestMaskStr(TestCase): From 6db44292598059d3493255d7cfce12b4eec4f727 Mon Sep 17 00:00:00 2001 From: Marco Marra Date: Thu, 15 Jan 2026 10:03:18 +0100 Subject: [PATCH 6/6] Add chengelog note --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffafec8..8b77774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Next Release +#### Fixes + +- `KeyError` when calling `changes_str` on a log entry that tracks many-to-many field changes ([#798](https://github.com/jazzband/django-auditlog/pull/798)) + ## 3.4.1 (2025-12-13) #### Fixes