A simple and flexible Django app for logging model actions and changes. This package allows developers to easily add logging capabilities to their Django models using decorators.
- 🚀 Easy to integrate with existing Django projects
- 🎯 Generic relations to work with any model
- 📝 Customizable log names and descriptions
- ⏰ Automatic timestamp tracking
- 🔍 Simple log retrieval
- 🎨 Clean and intuitive API
-
Install using pip:
-
Add 'model_logger' to your INSTALLED_APPS in settings.py:
-
Run migrations:
from django.contrib.contenttypes.models import ContentType
from model_logger.models import ModelLog
# Get all logs for a specific article
article = Article.objects.get(id=1)
logs = ModelLog.objects.filter(
content_type=ContentType.objects.get_for_model(Article),
object_id=article.id
)
# Get all logs across all models
all_logs = ModelLog.objects.all()
The ModelLog
model includes the following fields:
name
: The name of the logged actiondescription
: Detailed description of the actiontimestamp
: When the action occurredcontent_type
: The model type being loggedobject_id
: The specific object's IDcontent_object
: Generic foreign key to the logged object
class Article(models.Model):
title = models.CharField(max_length=100)
@log_action(
name="Article Published",
description="Article was published to the main page"
)
def publish(self):
self.is_published = True
self.save()
class Article(models.Model):
title = models.CharField(max_length=100)
@log_action(
name="Title Change",
description="Title changed from '{old_title}' to '{new_title}'"
)
def update_title(self, new_title):
old_title = self.title
self.title = new_title
self.save()
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Python 3.6+
- Django 3.2+
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any problems or have suggestions, please open an issue on GitHub.