django-field-history

raw JSON →
0.8.0 verified Fri May 01 auth: no python

A Django app to track changes to specific model fields. Version 0.8.0 is the latest. Releases are sporadic; no major changes since v0.6.0.

pip install django-field-history
error django.core.exceptions.ImproperlyConfigured: 'django_field_history.middleware.FieldHistoryMiddleware' is not a middleware class
cause The import path changed from field_history to django_field_history in v0.6.0. The old path may still be in middleware.
fix
Use 'django_field_history.middleware.FieldHistoryMiddleware' in MIDDLEWARE.
error django.db.migrations.exceptions.InconsistentMigrationHistory: Migration django_field_history.0001_initial is applied before its dependency django_content_type.0001_initial
cause Forgetting to include django.contrib.contenttypes in INSTALLED_APPS before running migrations.
fix
Add 'django.contrib.contenttypes' to INSTALLED_APPS and run migrations again.
gotcha Must also add 'django.contrib.contenttypes' to INSTALLED_APPS if not already present, otherwise migrations fail.
fix Ensure 'django.contrib.contenttypes' is in INSTALLED_APPS.
gotcha The FieldHistoryMiddleware uses the session to store the current user. If your site uses an authentication backend that does not set request.user correctly, history will be attributed to AnonymousUser.
fix Ensure authentication is set up so request.user is available; place FieldHistoryMiddleware after SessionMiddleware and AuthenticationMiddleware.

Enable field tracking by adding the app and middleware, then attach HistoricalRecords to your model with the fields you want to track.

# settings.py
INSTALLED_APPS = [
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django_field_history',
    # ...
]
MIDDLEWARE = [
    'django_field_history.middleware.FieldHistoryMiddleware',
    # ...
]

# models.py
from django.db import models
from django_field_history.models import HistoricalRecords

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    history = HistoricalRecords(fields=['name'])

# Run migrations and use admin to see history.