Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Marty Alchin's HistoricalRecords from the ProDjango book.

Project Description

This is from Marty Alchin’s Pro Django book.

Setup the virtualenv

$ virtualenv --no-site-packages ve
$ source ve/bin/activate
(ve)$ pip install -r requirements.pip

Or install from PyPI

$ pip install django-historicalrecords

Import the HistoricalRecords and attach it to your model like you would a custom Django manager.

from django.db import models
from history.models import HistoricalRecords

class TestModel(models.Model):
    """A model for testing"""
    boolean = models.BooleanField(default=True)
    characters = models.CharField(blank=True, max_length=100)

    history = HistoricalRecords()

    class Admin:
        list_display = ('',)
        search_fields = ('',)

    def __unicode__(self):
        return u"TestModel"

If you run syncdb you’ll see that it automatically creates a Historical version of whatever model you’ve attached it to.

(ve)$ ./ syncdb
Creating table auth_permission
... // snip // ...
Creating table example_app_historicaltestmodel <- HistoricalTestModel!
Creating table example_app_testmodel
... // snip // ...

The HistoricalRecords clone the model it is attached to and adds some extra fields that allow you to track the type of change made, the timestamp of when the change was saved and it has a descriptor that will return the original object at the time of the change.

(ve)$ ./ shell
>>> from example_app.models import TestModel
>>> tm = TestModel.objects.create(boolean=True,characters="abc")
>>> tm.history.count()
>>> most_recent = tm.history.most_recent()
>>> most_recent.boolean
>>> most_recent.characters
>>> tm.boolean = False
>>> tm.characters = "def"
>>> tm.history.count()
>>> tm.history.all()
[<HistoricalTestModel: TestModel as of 2010-09-10 03:29:59.424761>, <HistoricalTestModel: TestModel as of 2010-09-10 03:28:31.358548>]
>>> from datetime import datetime
>>> timestamp = datetime(2010,9,10,3,28,31,358548)
>>> old_version = tm.history.as_of(timestamp)
>>> old_version.boolean
>>> tm.boolean
>>> old_version.characters
>>> tm.characters

Release History

This version
History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(5.2 kB) Copy SHA256 Hash SHA256
Source None Feb 5, 2014

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting