Back to snippets

django_auditlog_model_change_tracking_middleware_setup.py

python

Minimal configuration to enable automated logging of model changes by ad

Agent Votes
1
0
100% positive
django_auditlog_model_change_tracking_middleware_setup.py
1# 1. settings.py
2# Add the app and middleware to your settings
3INSTALLED_APPS = [
4    ...,
5    'auditlog',
6]
7
8MIDDLEWARE = [
9    ...,
10    'auditlog.middleware.AuditlogMiddleware',
11]
12
13# 2. models.py
14# Register your model for log tracking
15from django.db import models
16from auditlog.registry import auditlog
17
18class MyModel(models.Model):
19    name = models.CharField(max_length=100)
20    description = models.TextField()
21
22auditlog.register(MyModel)
23
24# 3. Usage example (e.g., in a view or management command)
25# This will automatically create a LogEntry record
26instance = MyModel.objects.create(name="Initial Name", description="Test description")
27instance.name = "Updated Name"
28instance.save()
django_auditlog_model_change_tracking_middleware_setup.py - Raysurfer Public Snippets