django-model-changes-py3
PYTHON 3 FORK OF django-model-changes.
django-model-changes allows you to track the state and changes of a model instance:
Quick start
Install django-model-changes:
pip install django-model-changes-py3
Add “django_model_changes” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = ( ... 'django_model_changes', )Add the ChangesMixin to your model:
>>> from django.db import models >>> from django_model_changes import ChangesMixin >>> class User(ChangesMixin, models.Model): >>> name = models.CharField(max_length=100)
Get instance changes:
>>> user = User() >>> user.name = 'Foo Bar' >>> user.save() >>> user.name 'I got a new name' >>> # Get current state >>> user.current_state() {'id': 1, 'name': 'I got a new name'} >>> # Get previous state (state after previous save/create/delete) >>> user.previous_state() {'id': 1, 'name': 'Foo Bar'} >>> # Get old state (state before previous save/create/delete) >>> user.old_state() {'id': None, 'name': ''} >>> # Get changes from the previous state to the current state >>> user.changes() {'name': ('Foo Bar', 'I got a new name')} >>> # Get changes from the old state to the current state >>> user.old_changes() {'id': (None, 1), 'name': ('', 'Foo Bar')} >>> # Check if the instance was persisted (saved) >>> user.was_persisted() False >>> # Check if the instance is persisted >>> user.is_persisted() TrueListen for changes:
>>> from django_model_changes import post_change
>>> def my_callback(sender, instance, **kwargs): >>> # Do something with previous and current state >>> instance.old_state() >>> instance.current_state()
>>> # There is also a convenience method to get >>> # an instance from the previous state >>> instance.old_instance()
>>> post_change.connect(my_callback, User)
Overview
django-model-changes allows you to retrieve the following states from an instance:
- current_state()
The current state of the instance.
- previous_state()
The state of the instance after it was created, saved or deleted the last time.
- old_state()
The previous previous_state(), i.e. the state of the instance before it was created, saved or deleted the last time.
It also provides convenience methods to get changes between states:
- changes()
Changes from previous_state to current_state.
- previous_changes()
Changes from old_state to previous_state.
- old_changes()
Changes from old_state to current_state.
And the following methods to determine if an instance was/is persisted in the database:
- was_persisted()
Was the instance persisted in its old state.
- is_persisted()
Is the instance is_persisted in its current state.
This schematic tries to illustrate how these methods relate to each other:
after create/save/delete after save/delete now | | | .-----------------------------------.----------------------------------. |\ |\ |\ | \ | \ | \ | old_state() | previous_state() | current_state() | | | |-----------------------------------|----------------------------------| | previous_changes() (prev - old) | changes() (cur - prev) | |-----------------------------------|----------------------------------| | old_changes() (cur - old) | .----------------------------------------------------------------------. \ \ \ \ was_persisted() is_persisted()
Documentation
Refer to the doc strings in changes.py, or build the documentation:
>>> pip install Sphinx >>> cd docs >>> make html Open build/html/index.html
Metadata
Release files for django-model-changes-py3 0.15.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-model-changes-py3-0.15.1.tar.gz | 12.1 kB | Without provenance | Details for django-model-changes-py3-0.15.1.tar.gz |
Release files / django-model-changes-py3-0.15.1.tar.gz
| Download URL | django-model-changes-py3-0.15.1.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f49f30ed47d3e1cc8be3f07f69a8c6aae4c020325255cb2e2183aaf5000f469f
|
|
BLAKE2b-256 checksum How to use checksums |
0e28afaf74c2e60e465b96f09476df2f9fc52ee0b5ed6ebe3438d522ea926f7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-model-changes-py3 0.15.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.