Skip to content

Commit

Permalink
Fix m2m saved when simple history disabled (#1329)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
MattFanto and ddabble authored May 14, 2024
1 parent 23ef769 commit bf58d58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Authors
- `ddusi <https://github.com/ddusi>`_
- `DanialErfanian <https://github.com/DanialErfanian>`_
- `Sridhar Marella <https://github.com/sridhar562345>`_
- `Mattia Fantoni <https://github.com/MattFanto>`_

Background
==========
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
2 changes: 2 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bf58d58

Please sign in to comment.