From bf58d5863a4c8f276ad5a9aa64786646817717e7 Mon Sep 17 00:00:00 2001 From: Mattia Fantoni Date: Tue, 14 May 2024 23:08:54 +0200 Subject: [PATCH] Fix m2m saved when simple history disabled (#1329) * if statement to check settings * Updated changes * added reference to issue * added name as required * trigger test * Added test for #1329 and improved changelog --------- Co-authored-by: Anders <6058745+ddabble@users.noreply.github.com> --- AUTHORS.rst | 1 + CHANGES.rst | 2 ++ simple_history/models.py | 2 ++ simple_history/tests/tests/test_models.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index fcf285f7..37f060e0 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -141,6 +141,7 @@ Authors - `ddusi `_ - `DanialErfanian `_ - `Sridhar Marella `_ +- `Mattia Fantoni `_ Background ========== diff --git a/CHANGES.rst b/CHANGES.rst index 9f06150d..21e5f3d4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -25,6 +25,8 @@ Unreleased - Added a "Changes" column to ``SimpleHistoryAdmin``'s object history table, listing the changes between each historical record of the object; see the docs under "Customizing the History Admin Templates" for overriding its template context (gh-1128) +- Fixed the setting ``SIMPLE_HISTORY_ENABLED = False`` not preventing M2M historical + records from being created (gh-1328) 3.5.0 (2024-02-19) ------------------ diff --git a/simple_history/models.py b/simple_history/models.py index 86209847..5e09d466 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -673,6 +673,8 @@ def get_change_reason_for_object(self, instance, history_type, using): return utils.get_change_reason_from_object(instance) def m2m_changed(self, instance, action, attr, pk_set, reverse, **_): + if not getattr(settings, "SIMPLE_HISTORY_ENABLED", True): + return if hasattr(instance, "skip_history_when_saving"): return diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py index c2eb327b..c047bfc5 100644 --- a/simple_history/tests/tests/test_models.py +++ b/simple_history/tests/tests/test_models.py @@ -2411,6 +2411,22 @@ def test_skip_history(self): self.assertEqual(skip_poll.history.all().count(), 2) self.assertEqual(skip_poll.history.all()[0].places.count(), 2) + @override_settings(SIMPLE_HISTORY_ENABLED=False) + def test_saving_with_disabled_history_doesnt_create_records(self): + # 1 from `setUp()` + self.assertEqual(PollWithManyToMany.history.count(), 1) + + poll = PollWithManyToMany.objects.create( + question="skip history?", pub_date=today + ) + poll.question = "huh?" + poll.save() + poll.places.add(self.place) + + self.assertEqual(poll.history.count(), 0) + # The count should not have changed + self.assertEqual(PollWithManyToMany.history.count(), 1) + def test_diff_against(self): self.poll.places.add(self.place) add_record, create_record = self.poll.history.all()