Python

Django Audit Logging Guide

Add tamper-proof audit trails to your Django application with the AuditKit Python SDK. Middleware, signals, and model mixin support for comprehensive audit logging.

Overview

Django is the most widely used Python web framework for building database-backed applications. The AuditKit Django integration provides middleware for automatic request logging, model mixins for ORM-level change tracking, and signal handlers for capturing authentication events. It works with Django REST Framework (DRF) out of the box and supports multi-tenant applications using django-tenants or custom tenant isolation.

Getting started

1

Install the SDK

Add the AuditKit Python SDK with Django extras.

Python
pip install auditkit[django]
2

Configure in settings.py

Add AuditKit to your Django settings.

Python
# settings.py
INSTALLED_APPS = [
    ...
    'auditkit.django',
]

MIDDLEWARE = [
    ...
    'auditkit.django.middleware.AuditKitMiddleware',
]

AUDITKIT = {
    'API_KEY': os.environ['AUDITKIT_API_KEY'],
    'TENANT_RESOLVER': 'myapp.utils.get_tenant_id',
}
3

Add model mixin for change tracking

Use the AuditedModel mixin to automatically log model changes.

Python
from auditkit.django.mixins import AuditedModel

class Document(AuditedModel):
    title = models.CharField(max_length=255)
    content = models.TextField()
    classification = models.CharField(max_length=50)

    class AuditMeta:
        audit_fields = ['title', 'content', 'classification']
        # Automatically logs create, update, delete with field diffs
4

Log custom events

Use the AuditKit client for explicit event logging.

Python
from auditkit.django import get_auditkit

def approve_document(request, doc_id):
    auditkit = get_auditkit()
    document = Document.objects.get(id=doc_id)
    document.status = 'approved'
    document.save()

    auditkit.log(
        action='document.approved',
        actor={'id': str(request.user.id), 'email': request.user.email},
        target={'type': 'document', 'id': str(doc_id)},
        metadata={'title': document.title},
    )

Complete example

Here is a complete example showing AuditKit integrated into a Django application with authentication logging, data access tracking, and explicit event capture.

Python
# settings.py
INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'rest_framework',
    'auditkit.django',
    'myapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'auditkit.django.middleware.AuditKitMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]

AUDITKIT = {
    'API_KEY': os.environ['AUDITKIT_API_KEY'],
    'TENANT_RESOLVER': 'myapp.utils.get_tenant_id',
    'AUTO_LOG_AUTH': True,  # Log login/logout/failed attempts
    'AUTO_LOG_ADMIN': True,  # Log Django admin actions
}

# models.py
from auditkit.django.mixins import AuditedModel

class Invoice(AuditedModel):
    number = models.CharField(max_length=50, unique=True)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=20, default='draft')
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)

    class AuditMeta:
        audit_fields = ['amount', 'status']

# views.py
from auditkit.django import get_auditkit

class InvoiceViewSet(viewsets.ModelViewSet):
    def perform_update(self, serializer):
        instance = serializer.save()
        auditkit = get_auditkit()
        auditkit.log(
            action='invoice.updated',
            actor={'id': str(self.request.user.id)},
            target={'type': 'invoice', 'id': str(instance.id)},
            metadata={
                'invoice_number': instance.number,
                'new_status': instance.status,
            },
        )

Common patterns

AuditedModel mixin for automatic ORM change tracking

Middleware for HTTP request/response logging

Django signals for authentication event capture (login, logout, failed login)

Django admin action logging

Django REST Framework integration with ViewSet hooks

Multi-tenant support with django-tenants

Frequently asked questions

How do I add audit logging to a Django application?

Install auditkit[django], add auditkit.django to INSTALLED_APPS, add AuditKitMiddleware to MIDDLEWARE, and configure your API key in AUDITKIT settings. Use the AuditedModel mixin for automatic ORM change tracking or call auditkit.log() for explicit events.

Does AuditKit work with Django REST Framework?

Yes. AuditKit integrates with DRF through the standard middleware and provides ViewSet hooks for logging create, update, and delete operations with full request context.

More integration guides

Related resources

Add audit logging to your Django app

Get started with tamper-proof audit trails in minutes. Open source, from $99/mo.