Management command and middleware for Django history triggers.
Project description
django-history-triggers
django-history-triggers
is a Django application for installing database triggers
that automatically record inserts, updates, and deletes to model tables.
Requirements
- Django 3.0+
- PostgreSQL or SQLite database
Installation
pip install django-history-triggers
Quick Start
- Add
history
to your INSTALLED_APPS setting and migrate your database. - Add
history.middleware.HistoryMiddleware
to the end of yourMIDDLEWARE
setting. - Run
manage.py triggers enable
to install the trigger functions, ormanage.py triggers disable
to uninstall them. Neither will clear existing history data -- add a--clear
option to do that.
Settings
HISTORY_MODEL
(default:"history.ObjectHistory"
)HISTORY_IGNORE_APPS
(default:["admin", "contenttypes", "sessions"]
)HISTORY_MIDDLEWARE_IGNORE
(default:[]
)HISTORY_REQUEST_CONTEXT
(default:"history.get_request_context"
)HISTORY_ADMIN_ENABLED
(default:True
)
History Sessions
History is recorded within "sessions" that you can manage manually, either outside of a web request context, or in place of or in addition to the included middleware. The easiest way to manage a history session is via a context manager:
from history import get_backend
def api_view(request):
# You can pass extra fields to be stored for all history within a session.
with get_backend().session(user=request.user, path=request.path):
# All history inside here will have the same session_id and session_date.
...
Custom History Model
The default history.ObjectHistory
model is swappable by changing the HISTORY_MODEL
setting. If you need to define your own object history model (usually for tracking
custom fields or non-standard user info), be sure to inherit from
history.models.AbstractObjectHistory
. If at all possible, do this early on to avoid
problems with migrations when changing HISTORY_MODEL
after the initial migration.
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 Distributions
Built Distribution
Hashes for django_history_triggers-3.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58269c7f3862742340164d9715eecb26306b030e4ae356703e8121f2b6ab43fa |
|
MD5 | 14dd8f1029bbc549d7fc643dc3047cfe |
|
BLAKE2b-256 | 747c71e1b5805c80bd928a6ba866655562e38c3167e7a27ad4f843c3012444a0 |