Skip to content

Commit bf58d58

Browse files
MattFantoddabble
andauthored
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 <[email protected]>
1 parent 23ef769 commit bf58d58

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Authors
141141
- `ddusi <https://github.com/ddusi>`_
142142
- `DanialErfanian <https://github.com/DanialErfanian>`_
143143
- `Sridhar Marella <https://github.com/sridhar562345>`_
144+
- `Mattia Fantoni <https://github.com/MattFanto>`_
144145

145146
Background
146147
==========

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Unreleased
2525
- Added a "Changes" column to ``SimpleHistoryAdmin``'s object history table, listing
2626
the changes between each historical record of the object; see the docs under
2727
"Customizing the History Admin Templates" for overriding its template context (gh-1128)
28+
- Fixed the setting ``SIMPLE_HISTORY_ENABLED = False`` not preventing M2M historical
29+
records from being created (gh-1328)
2830

2931
3.5.0 (2024-02-19)
3032
------------------

simple_history/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ def get_change_reason_for_object(self, instance, history_type, using):
673673
return utils.get_change_reason_from_object(instance)
674674

675675
def m2m_changed(self, instance, action, attr, pk_set, reverse, **_):
676+
if not getattr(settings, "SIMPLE_HISTORY_ENABLED", True):
677+
return
676678
if hasattr(instance, "skip_history_when_saving"):
677679
return
678680

simple_history/tests/tests/test_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,22 @@ def test_skip_history(self):
24112411
self.assertEqual(skip_poll.history.all().count(), 2)
24122412
self.assertEqual(skip_poll.history.all()[0].places.count(), 2)
24132413

2414+
@override_settings(SIMPLE_HISTORY_ENABLED=False)
2415+
def test_saving_with_disabled_history_doesnt_create_records(self):
2416+
# 1 from `setUp()`
2417+
self.assertEqual(PollWithManyToMany.history.count(), 1)
2418+
2419+
poll = PollWithManyToMany.objects.create(
2420+
question="skip history?", pub_date=today
2421+
)
2422+
poll.question = "huh?"
2423+
poll.save()
2424+
poll.places.add(self.place)
2425+
2426+
self.assertEqual(poll.history.count(), 0)
2427+
# The count should not have changed
2428+
self.assertEqual(PollWithManyToMany.history.count(), 1)
2429+
24142430
def test_diff_against(self):
24152431
self.poll.places.add(self.place)
24162432
add_record, create_record = self.poll.history.all()

0 commit comments

Comments
 (0)