Django Audit Log
django-auditlog is a pluggable Django app that provides audit logging functionality for your models. It automatically tracks changes to model instances (create, update, delete actions), recording who made the change, when, and what changed. The current version is 3.4.1, and the project maintains an active release cadence with frequent updates to support new Django and Python versions.
Warnings
- breaking Python 3.9 support was dropped.
- breaking Django 4.1 support was dropped.
- breaking Log entry deletion behavior when primary keys are reused changed. Previously, if a model instance was deleted and a new instance was created with the same primary key, old log entries for that PK might have been deleted. This no longer happens.
- gotcha For Django 4.2+ environments, `thread.local` for context management was replaced with `ContextVar`. This is an internal change but might affect custom integrations that relied on `thread.local` for actor context propagation outside of the standard `AuditlogMiddleware`.
- gotcha Actor tracking (who performed the action) will result in `null` `actor` fields in `LogEntry` if `auditlog.middleware.AuditlogMiddleware` is not properly enabled in `MIDDLEWARE` in your `settings.py`, or if `auditlog.context.set_actor()` is not manually called.
- gotcha The `AUDITLOG_STORE_JSON_CHANGES` setting (introduced in v3.2.0) allows storing changes as JSON. Early versions (v3.2.0 to v3.2.1) had issues with updates/deletes and the `changes_display_dict` when this setting was `True`.
Install
-
pip install django-auditlog
Imports
- auditlog
from auditlog.registry import auditlog
- LogEntry
from auditlog.models import LogEntry
Quickstart
from django.db import models
from auditlog.registry import auditlog
class MyAuditedModel(models.Model):
name = models.CharField(max_length=255)
description = models.TextField(blank=True)
def __str__(self):
return self.name
class Meta:
app_label = 'myapp' # Replace 'myapp' with your actual app name
# Register the model for audit logging
auditlog.register(MyAuditedModel)