Skip to content

Commit

Permalink
docs: issue-10 📚 Add changed marker docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanier committed Nov 10, 2023
1 parent 0e2f1f6 commit 15a06dc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Using the `ChangeDetectionMixin` the pydantic models are extended, so:
changed fields.
**Note:** When using pydantic 1.x you need to use `obj.dict()` and `obj.json()`. Both
also accept `exclude_unchanged`.
* `obj.model_mark_changed("marker_name")` and `obj.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 in `obj.model_changed_markers`
as a set.

### Example

Expand Down Expand Up @@ -63,6 +67,31 @@ value to `model_set_changed()` when you want to also keep track of the actual ch
compared to the original value. Be advised to `.copy()` the original value
as lists/dicts will always be changed in place.

### Changed markers

You 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.

```python
import pydantic
from pydantic_changedetect import ChangeDetectionMixin

class Something(ChangeDetectionMixin, pydantic.BaseModel):
name: str


something = Something(name="something")
something.model_has_changed # = False
something.model_mark_changed("mood")
something.model_has_changed # = True
something.model_changed_markers # {"mood"}
something.model_unmark_changed("mood") # also will be reset on something.model_reset_changed()
something.model_has_changed # = False
```

# Contributing

If you want to contribute to this project, feel free to just fork the project,
Expand Down

0 comments on commit 15a06dc

Please sign in to comment.