diff --git a/AUTHORS.rst b/AUTHORS.rst index 16602a25..45e59a0e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -147,6 +147,7 @@ Authors - `Sridhar Marella `_ - `Mattia Fantoni `_ - `Trent Holliday `_ +- Rahul Mishra (`Senor-Ducky `_) Background ========== diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 7db701d9..a9104643 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.") diff --git a/simple_history/utils.py b/simple_history/utils.py index c79e93f7..34a49e91 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