Skip to main content

django-admin-audit

django-admin-audit is a reusable Django application that records actions in the Django admin along with request metadata (IP address, headers, user agent, and more). It plugs into Django's built-in admin logging so you can enrich existing audit trails without modifying your admin classes.

Features

  • Stores a normalized audit log for each admin action with optional request metadata.
  • Middleware-based request capture that supports IP anonymisation and header whitelisting.
  • Extensible hook for supplying extra JSON-serialisable context per action.
  • Ships with Django admin integration so logs are browsable immediately.
  • Superuser-only undo action that can revert additions, changes, and deletions when snapshots are available.

Installation

pip install django-admin-action-audit

Then add the app and middleware to your Django settings:

INSTALLED_APPS = [
    # ...
    "django_admin_audit",
]

MIDDLEWARE = [
    # ...
    "django_admin_audit.middleware.AdminAuditMiddleware",
]

Configuration

Customise the behaviour via the following optional settings (prefixed with ADMIN_AUDIT_):

Setting Default Description
ENABLED True Toggle the audit log without uninstalling the app.
ADMIN_PATH_PREFIXES ("/admin",) Path prefixes that should be considered admin traffic.
CAPTURE_HEADERS () Iterable of HTTP headers to store alongside each log entry.
IGNORE_IPS () Iterable of IP addresses to exclude from logging (e.g. health checks).
ANONYMIZE_IPS False Replace the last IPv4 octet with 0 for basic anonymisation.
EXTRA_CONTEXT_CALLABLE None Dotted path to a callable returning additional JSON-serialisable data per LogEntry.

Reverting admin actions

django-admin-audit records before/after snapshots for admin activity so that superusers can undo selected log entries from the Admin Audit Log changelist. The undo admin action honours the following rules:

  • Additions: removes the newly created object.
  • Changes: restores previously persisted field values.
  • Deletions: recreates the deleted object using the stored snapshot.

Snapshots are captured automatically for models managed through the Django admin while the audit middleware is active. If a snapshot is incomplete (for example, the object was already gone before the request), the entry is marked as non-revertible.

Providing extra context

Create a callable that accepts the LogEntry instance and returns a dictionary. The dictionary is stored in the extra JSON field.

# myproject/audit.py
def admin_extra_context(log_entry):
    return {"session_key": getattr(log_entry.user, "last_login_session", None)}

# settings.py
ADMIN_AUDIT_EXTRA_CONTEXT_CALLABLE = "myproject.audit.admin_extra_context"

Database migrations

This package ships with migrations. Run them after installation:

python manage.py migrate django_admin_audit

Running the tests

The test suite uses pytest and pytest-django:

pip install -e .[test]
pytest

Releasing

  1. Update django_admin_audit/__init__.py with the new version.
  2. Build the distribution: python -m build.
  3. Upload to TestPyPI first, then PyPI via twine upload.

License

This project is licensed under the MIT License. See LICENSE for details.

Metadata

Release files for django-admin-action-audit 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-admin-action-audit 0.1.0
File Size Uploaded
django_admin_action_audit-0.1.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-admin-action-audit 0.1.0
File Interpreter ABI Platform
django_admin_action_audit-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / django_admin_action_audit-0.1.0.tar.gz

Download URL django_admin_action_audit-0.1.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ea21f5ebb6efb15a5e135aae8fef67af07791d9986e5018eede41dc4fa23130b
BLAKE2b-256 checksum
How to use checksums
88f492933008e617e6110d51322aaa045509f80167cc52d6613b362c7cbf4904
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / django_admin_action_audit-0.1.0-py3-none-any.whl

Download URL django_admin_action_audit-0.1.0-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b0c21071879be947b30405ad4ac343e735b60221bc24c9236d7534eb1bda8dc
BLAKE2b-256 checksum
How to use checksums
f8233ea224dc411836fc555ebd7becb3b428de21f4c492643f201dde463730e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page