Package: pydantic-changedetect
Pydantic change detectionInstallationJust usepip install pydantic-changedetectto install the library.Note:pydantic-changedetectis compatible withpydanticversions1.9,1.10and even2.x(🥳) on
Python3.8,3.9,3.10and3.11. This is also ensured running all tests on all those versions
usingtox.AboutWhen working with database models it is pretty common to want to detect changes
to the model attributes. TheChangeDetectionMixinjust provides this mechanism
to any pydantic models. Changes will be detected and stored after the model
was constructed.Using theChangeDetectionMixinthe pydantic models are extended, so:obj.model_changed_fieldscontains a list of all changed fieldsobj.model_self_changed_fieldscontains a list of all changed fields for the
current object, ignoring all nested models.obj.model_changed_fields_recursivecontains a list of all changed fields and
also include the named of the fields changed in nested models using a
dotted field name syntax (likenested.field).obj.model_originalwill include the original values of all changed fields in
a dict.obj.model_has_changedreturns True if any field has changed.obj.model_set_changed()manually sets fields as changed.obj.model_set_changed("field_a", "field_b")will set multiple fields as changed.obj.model_set_changed("field_a", original="old")will set a single field as
changed and also store its original value.obj.model_reset_changed()resets all changed fields.obj.model_dump()andobj.model_dump_json()accept an additional parameterexclude_unchanged, which - when set to True - will only export the
changed fields.Note:When using pydantic 1.x you need to useobj.dict()andobj.json(). Both
also acceptexclude_unchanged.obj.model_restore_original()will create a new instance of the model containing its
original state.obj.model_get_original_field_value("field_name")will return the original value for
just one field. It will callmodel_restore_original()on the current field value if
the field is set to aChangeDetectionMixininstance (or list/dict of those).obj.model_mark_changed("marker_name")andobj.model_unmark_changed("marker_name")allow to add arbitrary change markers. An instance with a marker will be seen as changed
(obj.model_has_changed == True). Markers are stored inobj.model_changed_markersas a set.Exampleimportpydanticfrompydantic_changedetectimportChangeDetectionMixinclassSomething(ChangeDetectionMixin,pydantic.BaseModel):name:strsomething=Something(name="something")something.model_has_changed# = Falsesomething.model_changed_fields# = set()something.name="something else"something.model_has_changed# = Truesomething.model_changed_fields# = {"name"}original=something.model_restore_original()original.name# = "something"original.model_has_changed# = FalseWhen will a change be detectedpydantic-changedetectwill see changes when an attribute value is changes using an
attribute assignment likesomething.name = "something else"in the example above. Such
an assignment will be seen as a change unless the new value is the same and the type of
the value isNoneor of typestr,int,float,boolordecimal.Decimal.RestrictionsChangeDetectionMixincurrently cannot detect changes inside lists, dicts and
other structured objects. In those cases you are required to set the changed
state yourself usingmodel_set_changed(). It is recommended to pass the original
value tomodel_set_changed()when you want to also keep track of the actual changes
compared to the original value. Be advised to.copy()the original value
as lists/dicts will always be changed in place.importpydanticfrompydantic_changedetectimportChangeDetectionMixinclassTodoList(ChangeDetectionMixin,pydantic.BaseModel):items:list[str]todos=TodoList(items=["release new version"])original_items=todos.items.copy()todos.items.append("create better docs")# This change will NOT be seen yettodos.model_has_changed# = Falsetodos.model_set_changed("items",original=original_items)# Mark field as changed and store original valuetodos.model_has_changed# = TrueChanged markersYou may also just mark the model as changed. This can be done using changed markers.
A change marker is just a string that is added as the marker, models with such an marker
will also be seen as changed. Changed markers also allow to mark models as changed when
related data was changed - for example to also update a parent object in the database
when some children were changed.importpydanticfrompydantic_changedetectimportChangeDetectionMixinclassSomething(ChangeDetectionMixin,pydantic.BaseModel):name:strsomething=Something(name="something")something.model_has_changed# = Falsesomething.model_mark_changed("mood")something.model_has_changed# = Truesomething.model_changed_markers# {"mood"}something.model_unmark_changed("mood")# also will be reset on something.model_reset_changed()something.model_has_changed# = FalseContributingIf you want to contribute to this project, feel free to just fork the project,
create a dev branch in your fork and then create a pull request (PR). If you
are unsure about whether your changes really suit the project please create an
issue first, to talk about this.