Skip to main content

A Django app for advanced logging capabilities

Project description

Django Tracker

Django Tracker is a middleware for Django that automatically audits and logs all model changes made through user requests. It records the user responsible, the fields changed, and other useful information for tracking data changes.

Installation

pip install django-tracker-unergy

Setup

  1. Add 'django_tracker' to INSTALLED_APPS in your settings.py:
INSTALLED_APPS = [
    # ...
    'django_tracker',
]

and add the middleware

MIDDLEWARE = [
    # ...
    'django_tracker.middleware.CurrentUserMiddleware'
]
  1. Run migrations:
python manage.py makemigrations
python manage.py migrate django_tracker

Usage

Auditable Decorator

from django_tracker.decorators import auditable
from django_tracker.constants import AuditLevel

@auditable(
    tracked_fields=['field1', 'field2'],
    tracked_m2m_fields=['categories', 'tags'],  # NEW: Track M2M fields
    excluded_fields=["created_at", "updated_at"],
    audit_creates=True,
    audit_updates=True,
    audit_deletes=True,
    audit_m2m_changes=True,  # NEW: Enable M2M tracking
    level=AuditLevel.MEDIUM
)
class MyModel(models.Model):
    field1 = models.CharField(max_length=100)
    field2 = models.IntegerField()
    
    # Many-to-Many relationships that will be tracked
    categories = models.ManyToManyField('Category', blank=True)
    tags = models.ManyToManyField('Tag', blank=True)

Many-to-Many Field Tracking

Django-tracker now supports automatic tracking of Many-to-Many field changes:

Features:

  • Automatic detection of M2M additions, removals, and clears
  • Detailed logging of affected objects with their string representations
  • Signal-based tracking for real-time M2M changes
  • Configurable M2M field selection
  • Compatible with existing field tracking

M2M Log Structure:

{
    "categories": {
        "change_type": "many_to_many_signal",
        "field_name": "categories",
        "action": "post_add",
        "related_model": "Category",
        "affected_objects": [
            {"pk": 1, "model": "Category", "str_representation": "Technology"},
            {"pk": 2, "model": "Category", "str_representation": "Django"}
        ],
        "affected_count": 2
    }
}

Querying Audit Logs

You can view audit logs in the Django admin or directly from the model:

from django_tracker.models import AuditLog

# Get changes for a specific object
logs = AuditLog.objects.filter(
    content_type__model='mymodel',
    object_id=obj_id
)

# Get changes by user
logs = AuditLog.objects.filter(username='username')

# Get M2M changes specifically
m2m_logs = AuditLog.objects.filter(
    changes__has_key='categories'  # PostgreSQL
)

# Get specific M2M actions
add_logs = AuditLog.objects.filter(
    changes__categories__action='post_add'
)

License

MIT License. See LICENSE file 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_tracker_unergy-1.0.1.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

django_tracker_unergy-1.0.1-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file django_tracker_unergy-1.0.1.tar.gz.

File metadata

  • Download URL: django_tracker_unergy-1.0.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for django_tracker_unergy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f0b17627ce6a053890edd2b219b099be683edb8913d4a03d2b208b26faf176a0
MD5 a8b066bcf4e568352b0044171cd86d62
BLAKE2b-256 887a580654b93de8baa8feaa76875e7d1de9aceac95d098ce068b11aa4c1783c

See more details on using hashes here.

File details

Details for the file django_tracker_unergy-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tracker_unergy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46365e4bf8109ef552f0771bbbc69127af9a914cb24912a2f69200666d5b4d9f
MD5 be3e8a95a55fe7bb9f33f213da536d44
BLAKE2b-256 09ce70630293a7acdcb45413ea5c150017780d2df1409424a2e9cc791cc88d1e

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