【发布时间】:2013-07-25 15:02:44
【问题描述】:
我有包含不同内容(例如帖子、线程、cmets)的 Django webapp,我想跟踪它们的历史记录。所以我正在尝试为“内容”创建一个“版本控制”。我就是这样做的(问题跳到最后):
我有一个模型Create_Content,它代表一个动作:有一个演员 (editor)、一组修改 (edition)、一个时间戳以及它作用于的内容:
class Create_Content(models.Model):
editor = models.ForeignKey('User')
edition = models.TextField() # pickled dictionary
date = models.DateTimeField(auto_now_add=True)
content = models.ForeignKey('Content')
只保存一次,因为历史无法更改。
我定义的内容有:
class Content(models.Model):
last_edit = models.ForeignKey(Create_Content, related_name='content_last_id')
first_edit = models.ForeignKey(Create_Content, related_name='content_first_id')
def author(self):
return self.first_edit.editor
last_edit 是执行它的最后一个操作,first_edition 我用来获取内容的初始作者。
webapp 的所有实际内容都由 Content 的派生模型实现,这些派生模型由 Create_Content 的派生模型创建/编辑。例如,
class Comment(Content):
post = models.ForeignKey(Post, related_name="comment_set")
body = models.TextField()
在保存Create_Comment(Create_Content) 期间创建(实际上Create_Content 的所有子类都是代理)。
例如,用户版本是Create_Content 子类的实例(例如Edit_Comment(Create_Content)),在save() 期间,使last_edit 指向self 并更新实际内容(例如Edit_Comment.body )。
最后我正在为数据库做一个简化版的 Git:
-
Create_Content是提交(将完整状态存储在self.edition中)。 -
Content是一个特定的分支+工作目录(last_edit是指针), - 用户版本向前移动分支并将
Content更改为新内容(例如Comment的body)。
简短的结尾
我意识到每个用户操作都会在Create_Content 的表中有一个条目。
我的问题是:
这种情况有多糟糕?我的意思是,这张桌子可以很大,所有的动作都会碰到它。
我认为我的方法是“干净的”,但我很确定我正在重新发明轮子。这个特定的轮子是什么?
【问题讨论】:
-
Django-reversion 怎么样? github.com/etianen/django-reversion
标签: database django version-control orm django-models