马蒂·阿尔钦在普罗詹戈书中的历史记录。

django-historicalrecords-rca的Python项目详细描述


这是马蒂·阿尔钦的书。

设置virtualenv

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

或者从pypi安装

$ pip install django-historicalrecords

导入historicalrecords并将其附加到您的模型,就像您将其作为自定义django管理器一样。

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"

如果运行manage.py syncdb您将看到它会自动创建您附加到的任何模型的历史版本。

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

historicalrecords克隆它附加到的模型,并添加一些额外的字段,允许您跟踪所做更改的类型、保存更改时的时间戳,以及在更改时返回原始对象的描述符。

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
安卓为什么Java AudioEffect不支持双簧管?   增加内存后出现java IntelliJ堆大小错误   在unix/linux中工作的java中将unicode字符串转换为ASCII   java是否缺少正确对齐输出的值?   java Spring 3 MVC:动态表单中的onetomany(创建/更新时添加/删除)   java在接口中创建两个通用参数   lambda使用Java 8从嵌套列表中使用forEach查找项的替代方法是什么?   Java正则表达式匹配10位电话号码,中间有空格   linux将log4j外部化。使用命令行Java的属性文件   带有SSL的java简单RMI服务器   java无法为事务[…]打开JPA EntityManager无法获取驱动程序类“null”和URL“null”的连接   Android设备上的java Oauth Foursquare   for循环的ImageButton名称的java骰子模拟浓缩   java有没有简单的1d条形码阅读器?   如何在调用resultset后解决“无效字符串或缓冲区长度”。从java到访问mdb的getString()连接   在Java8中,是否可以使用JVM参数来控制何时(或在什么条件下)卸载类?