From aa5aa82c55cb9824f843adc02f7744ae583582b5 Mon Sep 17 00:00:00 2001 From: Senor-Ducky Date: Fri, 11 Apr 2025 12:13:39 +0530 Subject: [PATCH 1/3] fix history recored not being fetched properly if any model fields are null or default to None --- simple_history/tests/tests/test_utils.py | 10 ++++++++++ simple_history/utils.py | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 7db701d98..cc1915e3c 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -640,3 +640,13 @@ def test_update_change_reason_with_excluded_fields(self): update_change_reason(poll, "Test change reason.") most_recent = poll.history.order_by("-history_date").first() self.assertEqual(most_recent.history_change_reason, "Test change reason.") + + def test_update_change_reason_with_null_fields(self): + poll = PollWithExcludeFields( + question="what's up?", pub_date=timezone.now(), place=None + ) + poll.save() + update_change_reason(poll, "Test change reason.") + most_recent = poll.history.order_by("-history_date").first() + self.assertIsNone(most_recent.place) + self.assertEqual(most_recent.history_change_reason, "Test change reason.") \ No newline at end of file diff --git a/simple_history/utils.py b/simple_history/utils.py index c79e93f75..34a49e91f 100644 --- a/simple_history/utils.py +++ b/simple_history/utils.py @@ -19,7 +19,10 @@ def update_change_reason(instance, reason): if value is not None: attrs[field.attname] = value else: - attrs[field.attname] = value + if value is not None: + attrs[field.attname] = value + else: + attrs[f"{field.attname}__isnull"] = True record = history.filter(**attrs).order_by("-history_date").first() record.history_change_reason = reason From f9f1994ba6c9d6ba35729ed4b6239345d7e599af Mon Sep 17 00:00:00 2001 From: Senor-Ducky Date: Fri, 11 Apr 2025 12:16:44 +0530 Subject: [PATCH 2/3] add authors --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 16602a252..45e59a0ef 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -147,6 +147,7 @@ Authors - `Sridhar Marella `_ - `Mattia Fantoni `_ - `Trent Holliday `_ +- Rahul Mishra (`Senor-Ducky `_) Background ========== From fce1fb60bb2a7c9a761a0dd34ecb2ed84cb4a781 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Apr 2025 07:10:09 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- simple_history/tests/tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index cc1915e3c..a91046438 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -649,4 +649,4 @@ def test_update_change_reason_with_null_fields(self): update_change_reason(poll, "Test change reason.") most_recent = poll.history.order_by("-history_date").first() self.assertIsNone(most_recent.place) - self.assertEqual(most_recent.history_change_reason, "Test change reason.") \ No newline at end of file + self.assertEqual(most_recent.history_change_reason, "Test change reason.")