Audit Field Changes on Django Models
Project description
Audit Field Changes on Django Models
A Django app for auditing field changes on database models.
Installation
pip install django-field-audit
Documentation
Django Settings
Add the app to your Django INSTALLED_APPS
configuration to enable the app and
run migrations. Installed apps settings example:
INSTALLED_APPS = [
# ...
"field_audit",
]
If changed_by
auditing is desired (enabled by default), add the app middleware
to your Django MIDDLEWARE
configuration. For example:
MIDDLEWARE = [
# ...
"field_audit.middleware.FieldAuditMiddleware",
]
Custom settings
Name | Description | Default value when unset |
---|---|---|
FIELD_AUDIT_AUDITEVENT_MANAGER |
A custom manager to use for the AuditEvent Model. |
field_audit.models.DefaultAuditEventManager |
FIELD_AUDIT_FIELDCHANGE_MANAGER |
A custom manager to use for the FieldChange Model. |
django.db.models.Manager |
FIELD_AUDIT_AUDITORS |
A custom list of auditors for acquiring changed_by info. |
["field_audit.auditors.RequestAuditor", "field_audit.auditors.SystemUserAuditor"] |
Model Auditing
To begin auditing Django models, import the field_audit.audit_fields
decorator
and decorate models specifying which fields should be audited for changes.
Example code:
# flight/models.py
from django.db import models
from field_audit import audit_fields
@audit_fields("tail_number", "make_model", "operated_by")
class Aircraft(models.Model):
id = AutoField(primary_key=True)
tail_number = models.CharField(max_length=32, unique=True)
make_model = models.CharField(max_length=64)
operated_by = models.CharField(max_length=64)
Contributing
All feature and bug contributions are expected to be covered by tests.
Setup for developers
Create/activate a python virtualenv and install the required dependencies.
cd django-field-audit
mkvirtualenv django-field-audit # or however you choose to setup your environment
pip install django nose flake8 coverage
Running tests
-
Tests
nosetests
-
Style check
flake8 --config=setup.cfg
-
Coverage
coverage run -m nose coverage report -m
Adding migrations
The example manage.py
is available for making new migrations.
python example/manage.py makemigrations field_audit
Uploading to PyPI
Package and upload the generated files.
pip install -r pkg-requires.txt
python setup.py sdist bdist_wheel
twine upload dist/*
TODO
- Write backfill migration utility / management command.
- Write full library documentation using github.io.
- Switch to
pytest
to support Python 3.10. - Write
test_library.py
functional test module for entire library. - Add support for
QuerySet
write operations (update()
, etc). - Possibly remove
AutoField
accommodations and just forceBigAutoField
.
Backlog
- Don't use
threading.local()
for tracking the request. - Write model path change migration utility [maybe].
- Add to optimization for
instance.save(save_fields=[...])
[maybe]. - Support adding new audit fields on the same model at different times (instead
of raising
AlreadyAudited
) [maybe].
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
Hashes for django_field_audit-0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f73ee090f6f7ee2b9f53efe22298930c72c5b571ec494dc2bf905fedddf3ea9 |
|
MD5 | 8c2a5464daae2e3ed7a79ca7e9f86d1f |
|
BLAKE2b-256 | d27857caeb413553e72a23e904286ef9536f8e86937a656b159380f291296631 |