简单历史:django模型的历史
simple_histor的Python项目详细描述
django simple history是一个工具,用于在每次创建/更新/删除时存储db对象的状态。它已经在django 1.x中测试过(包括截至2011年8月30日的1.4)。
安装
下载tar.gz,解压缩并在目录中运行以下命令:
python setup.py install
基本用法
使用这个包是really简单的;您只需要import HistoricalRecords并在您希望历史跟踪的每个模型上创建它的实例。
在下面的行中添加MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES=('simple_history.middleware.CurrentUserMiddleware',)
在您的车型上,您需要在顶部包含以下行:
fromsimple_history.modelsimportHistoricalRecords
然后在您的模型类中,包括以下行:
history=HistoricalRecords()
然后从模型类或实例中,您可以访问history.all(),它将为您提供类的每个历史记录项或特定实例的每个历史记录项。
示例
classPoll(models.Model):question=models.CharField(max_length=200)pub_date=models.DateTimeField('date published')history=HistoricalRecords()classChoice(models.Model):poll=models.ForeignKey(Poll)choice=models.CharField(max_length=200)votes=models.IntegerField()history=HistoricalRecords()
$ ./manage.py shell
In[2]:frompoll.modelsimportPoll,ChoiceIn[3]:Poll.objects.all()Out[3]:[]In[4]:importdatetimeIn[5]:p=Poll(question="what's up?",pub_date=datetime.datetime.now())In[6]:p.save()In[7]:pOut[7]:<Poll:Pollobject>In[9]:p.history.all()Out[9]:[<HistoricalPoll:Pollobjectasof2010-10-2518:03:29.855689>]In[10]:p.pub_date=datetime.datetime(2007,4,1,0,0)In[11]:p.save()In[13]:p.history.all()Out[13]:[<HistoricalPoll:Pollobjectasof2010-10-2518:04:13.814128>,<HistoricalPoll:Pollobjectasof2010-10-2518:03:29.855689>]In[14]:p.choice_set.create(choice='Not Much',votes=0)Out[14]:<Choice:Choiceobject>In[15]:p.choice_set.create(choice='The sky',votes=0)Out[15]:<Choice:Choiceobject>In[16]:c=p.choice_set.create(choice='Just hacking again',votes=0)In[17]:c.pollOut[17]:<Poll:Pollobject>In[19]:c.history.all()Out[19]:[<HistoricalChoice:Choiceobjectasof2010-10-2518:05:30.160595>]In[20]:Choice.historyOut[20]:<simple_history.manager.HistoryManagerobjectat0x1cc4290>In[21]:Choice.history.all()Out[21]:[<HistoricalChoice:Choiceobjectasof2010-10-2518:05:30.160595>,<HistoricalChoice:Choiceobjectasof2010-10-2518:05:12.183340>,<HistoricalChoice:Choiceobjectasof2010-10-2518:04:59.047351>]
版权和许可证
版权所有(c)2012-2013 Pivotal Energy Solutions。保留所有权利。
根据apache许可证2.0版(以下简称“许可证”)授权; 除非符合许可证,否则您不得使用本作品。 您可以在许可证文件中获得许可证的副本,或访问:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则软件 根据许可证分发是按“原样”分发的, 无任何明示或默示的保证或条件。 有关管理权限的特定语言和 许可下的限制。