简体   繁体   English

如何使用 django-simple-history 恢复更改,特别是删除

[英]How to revert changes, specifically deletions, with django-simple-history

We have django-simple-history set up for our models.我们为我们的模型设置了django-simple-history 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.事后几天就注意到了这一点,因此最好避免完整的数据库备份恢复,因为这会清除事后发生的手动更改。

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.我可以使用 history - 1 上的实例属性来获取删除前的状态,但是如果我尝试保存它,它会出错,因为模型实例已被删除并且不再存在。

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?所以基本上,如果我有 django-simple-history 的历史记录,那么恢复已删除模型实例的最干净的方法是什么? I would like to retain the history if possible, so I am looking into any solution before totally recreating the objects.如果可能的话,我想保留历史记录,所以在完全重新创建对象之前我正在研究任何解决方案。

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.所以 Kal 的回答在这里没有帮助。 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").既然你有数据库的完整备份,你可以把这个数据库导入django环境,从这个备份数据库加载旧的模型实例并保存到生产数据库(在django shell中用“using”选择数据库)。 See this post.看到这个帖子。 The best way to avoid such situations is to use the app "reversion".避免这种情况的最好方法是使用应用程序“reversion”。 With this django-app, you actually can restore deleted instances.使用这个 django-app,您实际上可以恢复已删除的实例。 See the documentation .请参阅文档

Do you mean that your model, not just the instances , has been completely deleted?您的意思是您的模型,而不仅仅是实例,已被完全删除? If that's the case, it probably means some migration removed it.如果是这种情况,则可能意味着某些迁移将其删除。

You could try reverting the migration then restore from the Historical record.您可以尝试恢复迁移,然后从历史记录中恢复。

The previous version of your model is stored in the _HISTORICAL<model_name> table.模型的先前版本存储在 _HISTORICAL<model_name> 表中。 You could do a simple sql insert/select query to copy the data from that table back into the original table.您可以执行一个简单的 sql 插入/选择查询,将该表中的数据复制回原始表中。 Something like the below would work, but check the correct sql syntax for your database.像下面这样的东西可以工作,但请检查您的数据库的正确 sql 语法。

insert into app_mymodel (col_a, col_b) 
select col_a, col_b from app_HISTORICALmymodel 
where id in (1,2,3...)

If model using django-simple-history then,如果模型使用django-simple-history那么,

Data can be restored from the historical model.数据可以从历史模型中恢复。

We need to filter from the historical model.我们需要从历史模型中过滤。 for eg with some keyword or in some date range in which period the data got deleted accidentally.例如,使用某个关键字或在某个日期范围内,数据被意外删除。

For eg.例如。 the Model Name is Car , then by default django-simple-history creates a historical model as HistoricalCar .模型名称是Car ,然后默认情况下django-simple-history创建一个历史模型作为HistoricalCar

historical_data = HistoricalCar.objects.filter(history_type='-')
for each in historical_data:
   instance = each.instance  # This return instance of car.
   instance.pk = None  # Create New Instance
   instance.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM