如何使用django-simple-history恢复更改,特别是删除 [英] How to revert changes, specifically deletions, with django-simple-history

查看:148
本文介绍了如何使用django-simple-history恢复更改,特别是删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为模型设置了 django-simple-history .最近,一大堆模型被神秘地删除了.事实发生后几天就注意到了这一点,因此最好避免完整的数据库备份还原,因为那样会擦除事实之后发生的手动更改.

We have django-simple-history set up for our models. Recently a whole bunch of models were mysteriously deleted. This was noticed a few days after the fact, so it would be nice to avoid a full DB backup restore since that would wipe manual changes that happened after the fact.

我找不到任何方法来轻松还原模型实例,特别是已删除的实例.我可以查询模型的历史版本,并找到所有已删除的内容.这样一来,我还可以观察到它们都是上次更改时都已删除的内容.我可以使用历史记录上的instance属性-1来获取删除前的状态,但是如果我尝试保存该状态,则会出错,因为模型实例已删除且不再存在.

I cannot find any way to easily restore an model instance, specifically a deleted one. I can query the Historical version of the model and find everything that was deleted. With that I can also observe that all of them had deletions as their last change. I can use the instance property on history - 1 to get the state before deletion but if I try to save that it errors since the model instance was deleted and doesn't exist anymore.

因此,基本上,如果我具有django-simple-history的历史记录,那么还原已删除的模型实例的最干净的方法是什么?我想尽可能保留历史记录,因此在完全重新创建对象之前,我正在研究任何解决方案.

So basically, what is the cleanest way to restore a deleted model instance if I have the Historical record of it with django-simple-history? I would like to retain the history if possible, so I am looking into any solution before totally recreating the objects.

推荐答案

据我了解,问题是关于恢复已删除的模型 instance ,而不是类本身.因此,卡尔的答案在这里无济于事.要还原已删除的实例,不能使用简单的历史记录.根据文档,简单的历史记录只能恢复仍然存在的模型实例.由于您具有数据库的完整备份,因此可以将该数据库导入django环境,从该备份数据库中加载旧模型实例,然后将其保存到生产数据库中(使用"using"选择django shell中的数据库).请参阅帖子.避免此类情况的最佳方法是使用应用程序还原".使用此django-app,您实际上可以还原已删除的实例.请参见文档.

As I understand, the question, it is about restoring a deleted model instance, not the class itself. So Kal's answer does not help here. To restore a deleted instance, simple history can NOT be used. According to the documentation, simple history can only restore model instances that are still existing. Since you have a full backup of the database, you can import this database into the django environment, load the old model instance from this backup database and save it to the production database (choose the database in django shell with "using"). See this post. The best way to avoid such situations is to use the app "reversion". With this django-app, you actually can restore deleted instances. See the documentation.

这篇关于如何使用django-simple-history恢复更改,特别是删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆