Skip to content

Commit 8bcf4c4

Browse files
alpha1d3dCarlos-San-Emeteriocarlossel-gdsJonathan loo
authored
Hotfix/update change reason with excluded fields (#637)
* update_change_reason error with excluded fields If you have excluded fields update_change_reason generates an error, The instance will have values that won't match at line 18: history.filter(**attrs) Proposal change do avoid the error. * Test and minor additional correction. * Make compatible with other names for the history related field * Ran `make format` * Added authors and changes Co-authored-by: Carlos-San-Emeterio <[email protected]> Co-authored-by: carlos.sanemeterio <[email protected]> Co-authored-by: Jonathan loo <[email protected]>
1 parent 2131959 commit 8bcf4c4

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Authors
2020
- Brian Armstrong (`barm <https://github.com/barm>`_)
2121
- Buddy Lindsey, Jr.
2222
- Brian Dixon
23+
- Carlos San Emeterio (`Carlos-San-Emeterio <https://github.com/Carlos-San-Emeterio>`_)
2324
- Christopher Broderick (`uhurusurfa <https://github.com/uhurusurfa>`_)
2425
- Corey Bertram
2526
- Craig Maloney (`craigmaloney <https://github.com/craigmaloney>`_)
@@ -52,6 +53,7 @@ Authors
5253
- `jofusa <https://github.com/jofusa>`_
5354
- John Whitlock
5455
- Jonathan Leroy
56+
- Jonathan Loo (`alpha1d3d <https://github.com/alpha1d3d>`_)
5557
- Jonathan Sanchez
5658
- Jonathan Zvesper (`zvesp <https://github.com/zvesp>`_)
5759
- Josh Fyne

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
- Add setting to convert `FileField` to `CharField` instead of `TextField` (gh-623)
88
- import model `ContentType` in `SimpleHistoryAdmin` using `django_apps.get_model`
99
to avoid possible `AppRegistryNotReady` exception (gh-630)
10+
- Fix `utils.update_change_reason` when user specifies excluded_fields
1011

1112
2.8.0 (2019-12-02)
1213
------------------

simple_history/tests/tests/test_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
PollWithExcludeFields,
1212
Street,
1313
)
14-
from simple_history.utils import bulk_create_with_history
14+
from simple_history.utils import bulk_create_with_history, update_change_reason
1515

1616

1717
class BulkCreateWithHistoryTestCase(TestCase):
@@ -144,3 +144,14 @@ def test_bulk_create_no_ids_return(self, hist_manager_mock):
144144
hist_manager_mock().bulk_history_create.assert_called_with(
145145
objects, batch_size=None
146146
)
147+
148+
149+
class UpdateChangeReasonTestCase(TestCase):
150+
def test_update_change_reason_with_excluded_fields(self):
151+
poll = PollWithExcludeFields(
152+
question="what's up?", pub_date=now(), place="The Pub"
153+
)
154+
poll.save()
155+
update_change_reason(poll, "Test change reason.")
156+
most_recent = poll.history.order_by("-history_date").first()
157+
self.assertEqual(most_recent.history_change_reason, "Test change reason.")

simple_history/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ def update_change_reason(instance, reason):
88
attrs = {}
99
model = type(instance)
1010
manager = instance if instance.id is not None else model
11+
history = get_history_manager_for_model(manager)
12+
history_fields = [field.attname for field in history.model._meta.fields]
1113
for field in instance._meta.fields:
14+
if field.attname not in history_fields:
15+
continue
1216
value = getattr(instance, field.attname)
1317
if field.primary_key is True:
1418
if value is not None:
1519
attrs[field.attname] = value
1620
else:
1721
attrs[field.attname] = value
18-
history = get_history_manager_for_model(manager)
22+
1923
record = history.filter(**attrs).order_by("-history_date").first()
2024
record.history_change_reason = reason
2125
record.save()

0 commit comments

Comments
 (0)