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
- Update
django_admin_audit/__init__.pywith the new version. - Build the distribution:
python -m build. - Upload to TestPyPI first, then PyPI via
twine upload.
License
This project is licensed under the MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_admin_action_audit-0.1.0.tar.gz.
File metadata
- Download URL: django_admin_action_audit-0.1.0.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea21f5ebb6efb15a5e135aae8fef67af07791d9986e5018eede41dc4fa23130b
|
|
| MD5 |
c05020633b9e297fe806e967dc4dfcf9
|
|
| BLAKE2b-256 |
88f492933008e617e6110d51322aaa045509f80167cc52d6613b362c7cbf4904
|
File details
Details for the file django_admin_action_audit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_admin_action_audit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b0c21071879be947b30405ad4ac343e735b60221bc24c9236d7534eb1bda8dc
|
|
| MD5 |
673ef7ba8ca9516217d44b48829b1d24
|
|
| BLAKE2b-256 |
f8233ea224dc411836fc555ebd7becb3b428de21f4c492643f201dde463730e3
|