Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/null-none/django-logs
Log for your Django models
https://github.com/null-none/django-logs
audit django logs orm
Last synced: about 1 month ago
JSON representation
Log for your Django models
- Host: GitHub
- URL: https://github.com/null-none/django-logs
- Owner: null-none
- License: mit
- Created: 2023-09-25T19:56:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-05T13:23:17.000Z (about 1 year ago)
- Last Synced: 2024-11-01T18:11:44.369Z (2 months ago)
- Topics: audit, django, logs, orm
- Language: Python
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
============================
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 AuditLogclass 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]: [,
,
,
,
,
]