Skip to main content

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>]

Download files

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

Source Distribution

django-logs-1.0.0.tar.gz (8.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_logs-1.0.0-py2.py3-none-any.whl (9.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-logs-1.0.0.tar.gz.

File metadata

  • Download URL: django-logs-1.0.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for django-logs-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b0eb7f6628cee5203e3ab3fca2e8b8516bece6416396f9ae1c5e8247753544c0
MD5 8d3fa8ca0b054a3856e33e81a59845cc
BLAKE2b-256 c0792bec31e278fdbd273c42174e1dcf67fbc1c18f2cb9f59b46c681c43106c2

See more details on using hashes here.

File details

Details for the file django_logs-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_logs-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for django_logs-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 113f684b106a0455c56fec090c6d6b9588e7ade55dc50fe11b218366a35a25b9
MD5 d7b75c948b65a61a939faa8188117415
BLAKE2b-256 f5ccb62cec3d98a0d18d16ddf9d314ba22fa2ebc87f33faf50549cda7eeb6746

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