Skip to main content

django-logs

Tracking changes to django models.

  • Model fields for keeping track of the user and session that created and modified a model instance.

  • Abstract model class with fields created_by and modified_by fields.

  • A model manager class that can automatically track changes made to a model in the database.

Quickstart Guide

Install it with pip from PyPi:

pip install django-logs

Add logs.middleware.UserLoggingMiddleware to your MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    ...
    'logs.middleware.UserLoggingMiddleware',
)

To just track who created or edited a model instance just make it inherit from AuthStampedModel:

from logs.models import AuthStampedModel

class WarehouseEntry(AuthStampedModel):
    product = models.ForeignKey(Product)
    quantity = models.DecimalField(max_digits = 10, decimal_places = 2)

This will add 4 fields to the WarehouseEntry model:

  • created_by - A foreign key to the user that created the model instance.

  • created_with_session_key - Stores the session key with which the model instance was first created.

  • modified_by - A foreign key to the user that last saved a model instance.

  • modified_with_session_key - Stores the session key with which the model instance was last saved.

If you want to track full model change history you need to attach an AuditLog manager to the model:

from django.db import models
from logs.models.fields import LastUserField
from logs.models.managers import AuditLog


class ProductCategory(models.Model):
    name = models.CharField(max_length=150, primary_key = True)
    description = models.TextField()

    logs = AuditLog()

class Product(models.Model):
    name = models.CharField(max_length = 150)
    description = models.TextField()
    price = models.DecimalField(max_digits = 10, decimal_places = 2)
    category = models.ForeignKey(ProductCategory)

    logs = AuditLog()

You can then query the audit log:

In [2]: Product.logs.all()
Out[2]: [<ProductAuditLogEntry: Product: My widget changed at 2011-02-25 06:04:29.292363>,
        <ProductAuditLogEntry: Product: My widget changed at 2011-02-25 06:04:24.898991>,
        <ProductAuditLogEntry: Product: My Gadget super changed at 2011-02-25 06:04:15.448934>,
        <ProductAuditLogEntry: Product: My Gadget changed at 2011-02-25 06:04:06.566589>,
        <ProductAuditLogEntry: Product: My Gadget created at 2011-02-25 06:03:57.751222>,
        <ProductAuditLogEntry: Product: My widget created at 2011-02-25 06:03:42.027220>]

Metadata

Release files for django-logs 1.0.1

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-logs 1.0.1
File Size Uploaded
django-logs-1.0.1.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-logs 1.0.1
File Interpreter ABI Platform
django_logs-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 17.7 kB

Release files / django-logs-1.0.1.tar.gz

Download URL django-logs-1.0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a1148fcfeb93d29f8479be88b183f7f3cc236161aff7daa47bcce8dc96edab88
BLAKE2b-256 checksum
How to use checksums
4b10404c155eaed74a892748d37ee04eb899466ac08c5369eabaa562de742665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / django_logs-1.0.1-py2.py3-none-any.whl

Download URL django_logs-1.0.1-py2.py3-none-any.whl
Size 9.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c059d1fc886634c3fee24e9a4475b4f7df8453c1dcf1b8f085dd8bb2b6d15f8c
BLAKE2b-256 checksum
How to use checksums
7761ddad2dc92e94b44a05e355f14e0e4c294017b677974e8e7d0ca3bddeb38f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.9.0

2 release files

0.8.0

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