Skip to content

Fix update_change_reason: fails to fetch latest history entry for nullable fields #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Authors
- `Sridhar Marella <https://github.com/sridhar562345>`_
- `Mattia Fantoni <https://github.com/MattFanto>`_
- `Trent Holliday <https://github.com/trumpet2012>`_
- Rahul Mishra (`Senor-Ducky <https://github.com/Senor-Ducky>`_)

Background
==========
Expand Down
10 changes: 10 additions & 0 deletions simple_history/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
5 changes: 4 additions & 1 deletion simple_history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down