Logs for django models
Project description
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>]
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
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-logs-1.0.1.tar.gz.
File metadata
- Download URL: django-logs-1.0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1148fcfeb93d29f8479be88b183f7f3cc236161aff7daa47bcce8dc96edab88
|
|
| MD5 |
193168d04a77d6472c6123c803cfe949
|
|
| BLAKE2b-256 |
4b10404c155eaed74a892748d37ee04eb899466ac08c5369eabaa562de742665
|
File details
Details for the file django_logs-1.0.1-py2.py3-none-any.whl.
File metadata
- Download URL: django_logs-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c059d1fc886634c3fee24e9a4475b4f7df8453c1dcf1b8f085dd8bb2b6d15f8c
|
|
| MD5 |
eae215da35756a193761b5c9feff22e9
|
|
| BLAKE2b-256 |
7761ddad2dc92e94b44a05e355f14e0e4c294017b677974e8e7d0ca3bddeb38f
|