Skip to main content

Django Historical Records

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 manage.py syncdb you’ll see that it automatically creates a Historical version of whatever model you’ve attached it to.

(ve)$ ./manage.py 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)$ ./manage.py shell
>>> from example_app.models import TestModel
>>> tm = TestModel.objects.create(boolean=True,characters="abc")
>>> tm.history.count()
1
>>> most_recent = tm.history.most_recent()
>>> most_recent.boolean
True
>>> most_recent.characters
u'abc'
>>> tm.boolean = False
>>> tm.characters = "def"
>>> tm.save()
>>> tm.history.count()
2
>>> 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
True
>>> tm.boolean
False
>>> old_version.characters
u'abc'
>>> tm.characters
'def'
>>>

Metadata

Release files for django-historicalrecords-rca 1.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-historicalrecords-rca 1.2.5
File Size Uploaded
django-historicalrecords-rca-1.2.5.tar.gz 7.3 kB Details

Release files / django-historicalrecords-rca-1.2.5.tar.gz

Download URL django-historicalrecords-rca-1.2.5.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
268ae5097d5dd1c8a7cddc44f105abbe775b5c5f4a43d71cc54b596bf401c398
BLAKE2b-256 checksum
How to use checksums
a4863e724fd748cc5ad8d6dbccd9546f49a216ba208ad22c15888308f5de3398
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.2.5 This release

1 release file

1.2.4

1 release file

1.2.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page