A Django model field for PostgreSQL to store changes to a model chronologically.
AuditHistory model field to store a chronological record of changes to a model (“audit history”) on the model. The historical record is stored in a designated JSON field, so no additional database tables are required.
Supports Django 1.11 (and possibly Django 2.x in the future) and PostgreSQL database backends.
The basic principles are as follows:
To enable this for a model, you have to make these changes:
- add an
historyto the model
- add the
AuditHistoryMixinto the model class
- create ModelAdmin:
ModelAdmin(AuditHistoryAdminMixin, admin.ModelAdmin): pass
- add your ModelAdmin to admin site
- add an
Then, instead of calling regular
save()on the model after changing it, call
save_with_audit_record()instead (passing in some meta data you want saved alongside, e.g. the
eventthat caused the change, the
usertriggering it and some
payloadusually the set of modified fields.
The history will appear in human-readable form in the admin.
That’s pretty much all there is to it.
Testapp setup and first steps
- Install Postgres locally (e.g. 10.x)
- Create local database
audithistory, owned by user
- Create a virtualenv and activate:
virtualenv venv, then
- Install dependencies into virtualenv:
pip install --requirement requirements.txt
- Create new model on http://localhost:8000/admin/test_app/blogpost/
- Edit model via http://localhost:8000/edit/1/
- Reload admin page and inspect history record
Run tests in local environment:
manage.py test(Ensure that user dev has rights to db creation
alter user dev createdb;)
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for django-model-audit-history-1.1.2.tar.gz
Hashes for django_model_audit_history-1.1.2-py2-none-any.whl