An extension to the Django web framework that provides version control for model instances.
Project description
django-model-auditor
django-model-auditor is a Python library for maintaining versions of your django model instances.
Installation
Use the package manager pip to install foobar.
pip install django-model-auditor
Usage
- Add audits in INSTALLED_APPS inside settings.py of your django project
INSTALLED_APPS = [
'audits.apps.AuditsConfig',
]
- run migration to create auditlog model
python manage.py migrate
- Change metaclass of model(for which audit logs are needed) to ModelHistoryMeta
from audits.models import ModelHistoryMeta
from django.db import models
class ExampleModel(models.Model, metaclass=ModelHistoryMeta):
field1 = models.CharField(max_length=100)
field2 = models.CharField(max_length=100)
- Example
import ExampleModel
object = ExampleModel(field1="foo", field2="bar")
object.save() #will create an entry in auditlog model
object.field1 = "Foo"
object.field2 = "Bar"
object.save() #will create another entry in auditlog model
object.field1 = "FOO"
object.field2 = "BAR"
object.save() #will create another entry in auditlog model
#To get previous versions of the object
previous_versions = AuditLog.get_prev_versions(object, limit=2)
#By default get_prev_versions return only last_prev_version to
#get more versions we need to specify limit
latest_prev_version = previous_versions[0]
print("%s %s"%(latest_prev_version.field1, latest_prev_version.field2)) # "Foo","Bar"
prev_to_prev_version = previous_versions[1]
print("%s %s"%(prev_to_prev_version.field1, prev_to_prev_version.field2)) # "foo","bar"
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
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
Close
Hashes for django-model-auditor-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b0c31b6d92949c0c25fd3459f7e3dd44bf33f76842054b443179c45cf0d1215 |
|
MD5 | 8975b0a7174c1fda4cbe17d4e738e468 |
|
BLAKE2b-256 | 71205697e72db9a6788729b8eadcd6fb29ba71fa25f4b1c4a1aa8e7af4e9ed30 |
Close
Hashes for django_model_auditor-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 961986843c6b64887428bb74b797705b9a5dda82679fb27d4aaeaead754c36fd |
|
MD5 | 326d23fad1b01b23c502943196e74840 |
|
BLAKE2b-256 | f5254522971489cc8ed975162965c1bb313d4610abd02ddc92dc969424ed0511 |