Django Simple History
django-simple-history is a Django library that provides an easy way to store historical records for your Django models, allowing you to view and revert changes through the admin site. It is actively maintained with frequent releases, currently at version 3.11.0.
Warnings
- breaking The `simple_history_admin_list.display_list()` method was removed. If you were using this for custom admin views, it will break.
- breaking Support for Django 3.2 has been officially dropped. Projects using django-simple-history 3.7.0 or newer must upgrade their Django version.
- gotcha When defining `HistoricalRecords`, it must be instantiated (e.g., `history = HistoricalRecords()`). Forgetting the parentheses is a common error.
- gotcha Historical records for Many-to-Many (M2M) relationships require specific handling and are not tracked by default with simple `HistoricalRecords()`. Support for M2M with inheritance and signals was improved in 3.3.0.
- gotcha The repository moved from 'jazzband' to 'django-commons'. While not a direct code-breaking change, old documentation links, issue trackers, or GitHub references might be outdated.
Install
-
pip install django-simple-history
Imports
- HistoricalRecords
from simple_history.models import HistoricalRecords
- SimpleHistoryAdmin
from simple_history.admin import SimpleHistoryAdmin
- register
from simple_history import register
Quickstart
import os
from django.db import models
from simple_history.models import HistoricalRecords
# Ensure 'simple_history' is in INSTALLED_APPS in your Django settings.
# Example: INSTALLED_APPS = ['...', 'simple_history', 'myapp']
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
history = HistoricalRecords()
def __str__(self):
return self.name
# To see history in the Django admin:
# 1. Add 'simple_history' to INSTALLED_APPS.
# 2. In myapp/admin.py:
# from django.contrib import admin
# from simple_history.admin import SimpleHistoryAdmin
# from .models import Product
# admin.site.register(Product, SimpleHistoryAdmin)