Extend pydantic models to also detect and record changes made to the model attributes.
Project description
Pydantic change detection
Installation
Just use pip install pydantic-changedetect
to install the library.
About
When working with database models it is pretty common to want to detect changes
to the model attributes. The ChangeDetectionMixin
just provides this mechanism
to any pydantic models. Changes will be detected and stored after the model
was constructed.
Using the ChangeDetectionMixin
the pydantic models are extended, so:
obj.__changed_fields__
contains a list of all changed fieldsobj.__self_changed_fields__
contains a list of all changed fields for the current object, ignoring all nested models.obj.__changed_fields_recursive__
contains 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.__original__
will include the original values of all changed fields in a dict.obj.has_changed()
returns True if any field has changed.obj.set_changed()
manually sets fields as changed.obj.set_changed("field_a", "field_b")
will set multiple fields as changed.obj.set_changed("field_a", original="old")
will set a single field as changed and also store its original value.
obj.reset_changed()
resets all changed fields.obj.dict()
andobj.json()
accept an additional parameterexclude_unchanged
, which - when set to True - will only export the changed fields
Example
import pydantic
from pydantic_changedetect import ChangeDetectionMixin
class Something(ChangeDetectionMixin, pydantic.BaseModel):
name: str
something = Something(name="something")
something.has_changed # = False
something.__changed_fields__ # = set()
something.name = "something else"
something.has_changed # = True
something.__changed_fields__ # = {"name"}
Restrictions
ChangeDetectionMixin
currently cannot detect changes inside lists, dicts and
other structured objects. In those cases you are required to set the changed
state yourself using set_changed()
. It is recommended to pass the original
value to 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.
Contributing
If 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic-changedetect-0.2.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c0b30e4f14b68036675fbc46dea9c002822215c1ec59c1527d455be28cc013e |
|
MD5 | db9486bbb4ad11ed354da1a7cface452 |
|
BLAKE2b-256 | 7a3d3580f4675f2ed5287ecdec591b4de26c44325737cde41b3c8b6d71123606 |
Hashes for pydantic_changedetect-0.2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcdd637e4e5993617265cea65422b0dff7aa01c5a8d301fd431158e7e8376920 |
|
MD5 | d704a4e26fcec8ba9154f7a2b4e844de |
|
BLAKE2b-256 | 2815fec3eacddc415c3dbab6e76dd7914fc148d18832160cfbb2eadb567c3852 |