Historical records: set the change reason explicitly on the instance #8627
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The implementation of django-simple-history's
update_change_reason
is very brittle, as it queries for a previous historical record
that matches the attributes of the instance to update the
change_reason
,which could end up updating the wrong record, or not finding it.
https://github.com/jazzband/django-simple-history/blob/b8e43df626a03ce4817beaf6a20995b40a209f37/simple_history/utils.py#L27
I saw this mostly happening when:
find the record for some reason)
record instead)
But simple history also mentions that you can set the change reason
explicitly by setting the
_change_reason
attribute,which is more safer as it sets the change_reason at the creation time of
the record.
https://django-simple-history.readthedocs.io/en/latest/historical_model.html#change-reason
I have replaced almost all usages of
safe_update_change_reason
, except for the ones on apiv3readthedocs.org/readthedocs/api/v3/mixins.py
Lines 35 to 43 in dbe27b3
as we don't have access to the object before it's updated/created.
Also, maybe we can have a
set_change_reason
function instead, so it's less error-prone to get the attribute name wrong.