Skip to main content

Audit Django admin actions with request metadata.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_action_audit-0.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_action_audit-0.1.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_action_audit-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_admin_action_audit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea21f5ebb6efb15a5e135aae8fef67af07791d9986e5018eede41dc4fa23130b
MD5 c05020633b9e297fe806e967dc4dfcf9
BLAKE2b-256 88f492933008e617e6110d51322aaa045509f80167cc52d6613b362c7cbf4904

See more details on using hashes here.

File details

Details for the file django_admin_action_audit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_action_audit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0c21071879be947b30405ad4ac343e735b60221bc24c9236d7534eb1bda8dc
MD5 673ef7ba8ca9516217d44b48829b1d24
BLAKE2b-256 f8233ea224dc411836fc555ebd7becb3b428de21f4c492643f201dde463730e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page