Skip to main content

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

  1. Add audits in INSTALLED_APPS inside settings.py of your django project
INSTALLED_APPS = [
    'audits.apps.AuditsConfig',
]
  1. run migration to create auditlog model
python manage.py migrate
  1. 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)
  1. 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

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-model-auditor-0.0.4.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

django_model_auditor-0.0.4-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page