[Not intended for use] Historical records #1154
Draft
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.
DEPRECATED
Details
Single-table implementation of historical records with associated historical querysets, historical managers and diff methods via signals and mixins.
Intended to collapse current models and their assoc. historical models to avoid multiple joins. In this implementation, we have reduced the number of tables that the Concept implementation from 14 tables down to 3 tables (technically 5 with the relational tables to handle M2M fields).
Related issue
Relates to following issue:
Example
In this implementation of Concepts, one could retrieve the final codelist in a manner similar to:
Implementation
Version ID handling
Versions are sequential for each instance instead of a global increment.
Relational support
Supports relational fields, e.g. M2M fields - can be seen within the
concept_migrate
command files.Basic features
Setup
Simply extend the historical model mixin:
Historic fields
Each historic model includes:
entity_id
i.e. the id of the instanceversion_id
i.e. sequential version id for each entity instance (distinct on entity_id)version_date
i.e. this version's creation datecreated_date
i.e. when the instance was first createdchange_type
i.e. whether this version was created/edited/deletedchange_reason
i.e. why this entity was changedHistoric methods
is_historic()
Used to determine whether an entity is the current version or a historic version, e.g.:
HistoricalQuerySets & HistoricalManagers
Querying only the current objects
Can query only the current objects via the
.objects
property, e.g.:Querying history by class
Ability to filter history of all instances within a model
Querying history by instance
Ability to filter history of a specific instance of a model
HistoricQuerySet methods
latest_of_each()
Used to find the latest version of each entity in a queryset, e.g.:
HistoricManager methods
most_recent()
Returns the most recent record of an instance, e.g.:
earliest()
Returns the earliest record of an instance, e.g.:
Versioning & Diffing
Diff methods
get_delta()
Returns the diff between two instances, e.g.:
get_field_diff()
Returns the diff of a specific field, e.g.:
Diff properties
_dict
Attempts to dictify the model for comparison, e.g.:
has_changed
Returns boolean that represents whether the object has been changed since it was last saved, e.g.
changed_fields
Returns a list of names describing the fields that have changed since it was last saved, e.g.:
diff
Returns the dict that describes the diff of now v.s. when the instance was last saved, e.g.: