Django模型中用于记录更改的模型和混音
django-record的Python项目详细描述
安装
pip install django-record
基本原理
通常情况下,您需要记录模型的属性和 你想跟踪他们变化的地方。尽管记录过程可以实现 通过handcrafted, ad-hoc signals或overriding save() methods of your models,它是 不是一般的方法,它最终会弄乱您的代码库。
django record在审核时自动创建类似快照的额外记录 django模型实例已被直接或间接更改, 不会搞乱你的代码库。
RecordModel将检测的recording_fields中的任何更改 recording_model现在是post_save时间或auditing_relatives时间 post_savetime并为其创建新记录。
您可以通过录制模型中的记录管理器records访问记录 实例。您还可以通过recording访问录制模型的实例, 它实际上只是普通的ForeignKey,从您的记录中。
更方便的是,只需将RecordedModelMixin混合到您的模型中,并提供 recording_fields和auditing_relatives。
用法
fromdjango.dbimportmodelsfromdjango_record.mixinsimportRecordedModelMixinclassMyTopic(models.Model):title=models.CharField(max_length=100)classMyArticle(RecordedModelMixin,models.Model):topic=models.ForeignKey(MyTopic)text=models.TextField()@propertydefmy_local_property(self):returnself.text@propertydefmy_nonlocal_property(self):returnself.topic.title+self.text# We will monitor `topic` relative to watch if he changes!auditing_relatives=['topic']recording_fields=[# Record changes of the model instance's `text` field'text',# Yayy! we can record changes on properties too!('my_local_property',models.TextField()),# Even indirect effects from relatives are recordable!('my_nonlocal_property',models.TextField())]# To get the model instance's all records>>>my_article.records.all()# To get queryset of the model instance's records created in specific# time threshold>>>my_article.records.created_in_years(2)>>>my_article.records.created_in_days(3)>>>my_article.records.created_in_minutes(5)# To resample records of today by hour>>>my_article.records.created_in_days().resample('T')# To get record contents>>>my_article.records.first().text>>>my_article.records.first().my_local_property>>>my_article.records.first().my_nonlocal_property
注
- 当前不支持递归审核。仅间接影响那些 将从直系亲属处发现并记录。
- 属性只支持基元类型。必须为它们提供适当的django字段。
- RecordModel也是TimeStampedModel的子类,因此请确保 不记录“创建”或“修改”字段。
变化
2015年9月11日(0.2.5版)
- 将TimestampedModel重命名为AbstractTimestampedModel。
- 用RecordQueryset筛选器替换了猴子修补的快捷方式属性。 现在不推荐使用这些快捷方式属性。
2015年9月20日
- 支持Python3.4
2015年6月13日
- 以下快捷方式属性添加到recording_model。所有属性
下面是普通的django querysets.
- records_in_hour:最近一小时内创建的记录。
- records_in_day:今天创建的记录。
- records_in_week:本周创建的记录。
- records_in_month:本月创建的记录。
- records_in_year:今年创建的记录。
- 以下提供重新采样记录的快捷方式属性已添加到
录制模式。下面的所有属性都是普通的django queryset。
- resampled_records_in_hour:最近一小时内创建的记录, 用分钟重新取样。
- resampled_records_in_day:今天创建的记录,用小时重新采样。
- resampled_records_in_week:本周创建的记录,用 天。
- resampled_records_in_month:本月创建的记录,重新取样 一天一天。
- resamped_records_in_year:今年创建的记录,使用 月。
2015年5月18日
- RecordedModelMixin已添加。