Skip to main content

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 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'
>>>

Project details


Download files

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

Source Distribution

Built Distribution

File details

Details for the file django-historicalrecords-unleashed-1.3.1.tar.gz.

File metadata

File hashes

Hashes for django-historicalrecords-unleashed-1.3.1.tar.gz
Algorithm Hash digest
SHA256 04d8547caee3cb9c3dc55839e6e1a88ae560aad8d0e260e21cabea1dc0f1ed99
MD5 f55be246e8cabca37a2a2eb51c7316c2
BLAKE2b-256 8e5e5632c35be298bc337cd6f2abfed193b8d5d646ec1dc89fcd9229e8a00937

See more details on using hashes here.

File details

Details for the file django_historicalrecords_unleashed-1.3.1-py2-none-any.whl.

File metadata

File hashes

Hashes for django_historicalrecords_unleashed-1.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 be67f4e05c24ccba50e3b2e3a34011b1ba3f6dcd3ca8c4af965698d2de73d142
MD5 3ea269faeca14a696ca66d755caf8d34
BLAKE2b-256 18dd0b51aa0317af4f298518f2200936141bf4b83f8f67f22b4b698abc0496dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page