Open
Conversation
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] – Fix historical tracking for dynamically added model fields
Description
Context:
Prior to this change,
django-simple-historysuffered from a timing limitation where dynamically created fields were not being tracked. Historical models are generated by copying the concrete model's fields during the Djangoclass_preparedsignal. However, custom metaclasses (such asdjango-organizations'OrgMeta) often add fields (like ForeignKey relationships) to models after theclass_preparedhook has fired. Consequently, these late-added fields were entirely omitted from the generated historical tracking models, breaking history functionality for those fields.Approach:
This fix introduces a hybrid re-finalization pattern to ensure all fields are captured without disrupting the framework's startup timing dependency on settings:
HistoricalRecords.finalize()method to continue building historical models immediately uponclass_prepared, preserving reliance on module-level settings (likeSIMPLE_HISTORY_HISTORY_ID_USE_UUID). However, it now records a field count snapshot in_finalized_models._finalized_models. If a model's current field count differs from its snapshot (indicating dynamically added fields), its historical model is safely re-created to incorporate the new fields.Impact:
class_prepared) are now correctly included and tracked in historical models.Visual Proof / Evidence
Tests
Unit Tests Summary:
Created the DynamicFieldTrackingTest class containing 5 new end-to-end tests: