Fix #1582: Include custom base model fields in diff_against()#1593
Open
ace2016 wants to merge 1 commit intodjango-commons:masterfrom
Open
Fix #1582: Include custom base model fields in diff_against()#1593ace2016 wants to merge 1 commit intodjango-commons:masterfrom
ace2016 wants to merge 1 commit intodjango-commons:masterfrom
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
[Improvement] – Include custom base model fields in
diff_against()deltasDescription
Context:
Previously, fields added via a custom base class (passed using the
basesparameter ofHistoricalRecords) were excluded from the results ofdiff_against(). This happened becausediff_against()usedtracked_fieldsas its default set of included fields, andtracked_fieldsis explicitly designed by the library to only include the fields present on the original model. As a result, custom base fields (like custom session tracking or IP address tracking columns) would silently be omitted from history diffs, even when their values clearly changed between versions (Issue #1582).Approach:
included_fieldslogic withinHistoricalChanges.diff_against()insimple_history/models.py.tracked_fields, the method now cleanly scans the historical model's_meta.fieldsfor any editable fields that are not already tracked and are not internal history metadata (e.g.,history_id,history_date,history_type, etc.).test_history_diff_includes_custom_base_fields) insimple_history/tests/tests/test_models.pyleveraging the existingPollWithHistoricalSessionAttrtest model.Impact:
diff_against()now correctly identifies and includes changes to custom base fields (e.g.,sessionorip_addressfields added viabases) in its returnedModelDeltaobjects.tracked_fieldsdefinition, ensuring that other features reliant ontracked_fields(likemost_recent()andbulk_history_create()) remain completely unaffected and safe.Visual Proof / Evidence
The entire test suite ran successfully, including the new regression test, proving the fix works precisely as intended and maintains full backwards compatibility with all existing diff behaviors.
Tests
Unit Tests Summary:
test_history_diff_includes_custom_base_fieldsinsimple_history/tests/tests/test_models.py: Leverages thePollWithHistoricalSessionAttrmodel to create snapshots with different custom field values (session-oldandsession-new). Callingdiff_against()asserts that thesessionfield name safely populates intodelta.changed_fields, and validates that its old and new values correctly appear insidedelta.changes.