开发者

Keeping track of changes - Django

开发者 https://www.devze.com 2022-12-24 03:04 出处:网络
I have various models of which I would like to keep track and collect statistical data. The problem is how 开发者_StackOverflow中文版to store the changes throughout time.

I have various models of which I would like to keep track and collect statistical data.

The problem is how 开发者_StackOverflow中文版to store the changes throughout time.

I thought of various alternative:

  • Storing a log in a TextField, open it and update it every time the model is saved.
  • Alternatively pickle a list and store it in a TextField.
  • Save logs on hard drive.

What are your suggestions?


Don't reinvent the wheel.. Use django-reversion for logging changes.

I'd break statistics off into a separate model though.


Quoth my elementary chemistry teacher: "If you don't write it down, it didn't happen", therefore save logs in a file.

Since the log information is disjoint from your application data (it's meta-data, actually), keep them separate. You could log to a database table but it should be distinct from your model.

Text pickle data is difficult for humans to read, binary pickle data even more so; log in an easily parsed format and the data can be imported into analysis software easily.


I've had similar situation in which we were supposed to keep the history of changed. But we also needed audit to track who made the changes and the ability to revert. In our approach storing in database seemed more logical. However considering you have statistical data and it's gonnna be large, perhaps separate file based approach would be better for you.

In any case you should use a generic mechanism to log the changes on models rather than coding each model invidually.

Take a look at this: http://www.djangosnippets.org/snippets/1052/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号