Simple-history: History for Django Models
django-simple-history is a tool to store state of DB objects on every create/update/delete. It has been tested to work in django 1.X (including 1.4 as of 08/30/2011).
Download the tar.gz, extract it and run the following inside the directory:
python setup.py install
Using this package is really simple; you just have to import HistoricalRecords and create an instance of it on every model you want to historically track.
Append the following line you your MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = ( 'simple_history.middleware.CurrentUserMiddleware', )
On your models you need to include the following line at the top:
from simple_history.models import HistoricalRecords
Then in your model class, include the following line:
history = HistoricalRecords()
Then from either the model class or from an instance, you can access history.all() which will give you either every history item of the class, or every history item of the specific instance.
class Poll(models.Model): question = models.CharField(max_length = 200) pub_date = models.DateTimeField('date published') history = HistoricalRecords() class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) votes = models.IntegerField() history = HistoricalRecords()
$ ./manage.py shell
In : from poll.models import Poll, Choice In : Poll.objects.all() Out:  In : import datetime In : p = Poll(question="what's up?", pub_date=datetime.datetime.now()) In : p.save() In : p Out: <Poll: Poll object> In : p.history.all() Out: [<HistoricalPoll: Poll object as of 2010-10-25 18:03:29.855689>] In : p.pub_date = datetime.datetime(2007,4,1,0,0) In : p.save() In : p.history.all() Out: [<HistoricalPoll: Poll object as of 2010-10-25 18:04:13.814128>, <HistoricalPoll: Poll object as of 2010-10-25 18:03:29.855689>] In : p.choice_set.create(choice='Not Much', votes=0) Out: <Choice: Choice object> In : p.choice_set.create(choice='The sky', votes=0) Out: <Choice: Choice object> In : c = p.choice_set.create(choice='Just hacking again', votes=0) In : c.poll Out: <Poll: Poll object> In : c.history.all() Out: [<HistoricalChoice: Choice object as of 2010-10-25 18:05:30.160595>] In : Choice.history Out: <simple_history.manager.HistoryManager object at 0x1cc4290> In : Choice.history.all() Out: [<HistoricalChoice: Choice object as of 2010-10-25 18:05:30.160595>, <HistoricalChoice: Choice object as of 2010-10-25 18:05:12.183340>, <HistoricalChoice: Choice object as of 2010-10-25 18:04:59.047351>]
Copyright and license
Copyright (c) 2012-2013 Pivotal Energy Solutions. All rights reserved.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.