简体   繁体   English

Django还原

[英]Django Reversion

I want to undo my last POST/PUt/DELETE or any sort of transaction within a database. 我想撤消我上一次的POST / PUt / DELETE或数据库中的任何类型的事务。 I read about Django-reversion & tried implementing it. 我阅读了有关Django-reversion的文章,并尝试实现它。 But to no luck. 但是,没有运气。 Here's my code. 这是我的代码。 Please confirm what wrong am I doing!!! 请确认我在做什么错!!!

Models to be reversed :- 待逆转的模型:-

@reversion.register
class Shipment(models.Model):
    job_id = models.CharField(max_length = 255)
    time = models.DateTimeField( auto_now_add = True, db_index = True)

@reversion.register
class ShipmentScanMapping(models.Model):
    arm_id = models.CharField(max_length = 255)
    status = models.CharField(max_length = 255)
    barcode = models.CharField(max_length = 255)
    reference_number = models.CharField(max_length = 255)
    customer_name = models.CharField(max_length = 255)
    shipment = models.ForeignKey('Shipment',related_name ='scans')
    time = models.DateTimeField( auto_now_add = True)

Views 查看

@csrf_exempt
@reversion.create_revision()
def db_commit(request):

    arm_id = scan_status = barcode = reference_number = customer_name = job_id = 12
    if request.is_ajax():
        shipment_obj = Shipment(job_id = job_id)
        shipment_obj.save()

        ShipmentScanMapping.objects.create(arm_id = arm_id,status = scan_status,barcode = barcode,reference_number = reference_number,
                                                customer_name = customer_name ,shipment= shipment_obj)

        return HttpResponse(json.dumps('Success'), content_type = "application/json")        

@csrf_exempt
def db_undo(request):
    job_id = 12 # just for demo purpose
    shipment_obj = Shipment.objects.filter(job_id = job_id).order_by('-time')[0]

    your_model = ShipmentScanMapping.objects.get(shipment = shipment_obj)
    version_list = reversion.get_unique_for_object(your_model)[0]
    #version_data = version_list.field_dict
    #print version_data
    version_list.revert()

    return HttpResponse(json.dumps('Success'), content_type = "application/json")        

What wrong am I doing ?? 我在做什么错?

In your example you just create new object. 在您的示例中,您只需创建一个新对象。 In this case django-reversion create initial state reversion for this object, but django-reversion allows you to roll back to the state of any previous revision. 在这种情况下, django-reversion为此对象创建初始状态还原,但是django-reversion允许您回滚到任何先前修订的状态。 So modify your ShipmentScanMapping object and try again. 因此,修改ShipmentScanMapping对象,然后重试。

Update 更新

Initialize data: 初始化数据:

>>> import reversion
>>> with reversion.create_revision():
...     shipment_obj = Shipment(job_id = 12)
...     shipment_obj.save()
...
>>> reversion.get_unique_for_object(shipment_obj)
[<Version: Shipment object>]
>>> with reversion.create_revision():
...     your_model = ShipmentScanMapping.objects.create(arm_id = 12,status = 12,barcode = 12,reference_number = 12,customer_name =12,shipment=shipment_obj)
...
>>> reversion.get_unique_for_object(your_model)
[<Version: ShipmentScanMapping object>]
>>> your_model.arm_id
12
>>> your_model.id
1

Update data: 更新数据:

>>> with reversion.create_revision():
...     your_model.arm_id=15
...     your_model.save()
...
>>> reversion.get_unique_for_object(your_model)
[<Version: ShipmentScanMapping object>, <Version: ShipmentScanMapping object>]
>>> ShipmentScanMapping.objects.get(id=1).arm_id
15
>>> [r.field_dict.get('arm_id') for r in reversion.get_unique_for_object(your_model)]
[u'15', u'12']

Revert: 还原:

>>> rev = reversion.get_unique_for_object(your_model)[-1]
>>> rev.field_dict.get('arm_id')
u'12'
>>> rev.revert()
>>> ShipmentScanMapping.objects.get(id=1).arm_id

Update2 UPDATE2

Can I revert only those changes which have been updated(PUT) or any POST/DELETE request can also be revert? 我可以只还原那些已更新的更改(PUT),还是可以还原任何POST / DELETE请求?

You can also recover a deleted object, see docs . 您还可以恢复已删除的对象,请参阅docs

How can I revert upto nth level?? 如何恢复到第n级? Does [-1] means the DB will be reverted to the latest update? [-1]是否意味着数据库将还原为最新更新?

get_unique_for_object method returns a list of all previous versions, latest versions first. get_unique_for_object方法将返回所有先前版本的列表,最新版本优先。 So you can select as usual selection from the list in python. 因此,您可以像往常一样从python列表中进行选择。 By the way you can find the most recent version for a given date: 顺便说一下,您可以找到给定日期的最新版本:

rev = reversion.get_for_date(your_model, datetime.datetime(2008, 7, 10))

Update3 UPDATE3

if initially, the value was 3 and was updated with 12 and then reverted back to 3. What happens with the value 12?? 如果最初是3,则将其更新为12,然后又恢复为3。值12会发生什么? Also, how would I revert back an insert(POST) query, eg:- if I inserted 12 and then I want to revert this change? 另外,我该如何还原回一个insert(POST)查询,例如:-如果我插入了12,然后又想还原此更改?

Okay, let's consider how it works. 好的,让我们考虑一下它是如何工作的。 When you register your model, the django-reversion begins to watch post_save signals from the model. 注册模型时, django-reversion开始监视来自模型的post_save信号。 Whenever you save changes to a model, it is serialized using the Django serialization framework into a JSON string and save it to the database as a reversion.models.Version model. 每当您保存对模型的更改时,都会使用Django序列化框架将其序列化为JSON字符串,并将其作为reversion.models.Version模型保存到数据库中。

So when you need reverting some version, django-reversion load the appropriate Version model from the database, deserializing the model data, and re-saving the old data. 因此,当您需要还原某些版本时, django-reversion从数据库中加载适当的Version模型,反序列化模型数据,然后重新保存旧数据。 During the re-saving the current state will be lost if not called method save. 在重新保存期间,如果不调用方法保存,则当前状态将丢失。 If it was called, the state will be stored in appropriate version(see text above). 如果调用了该状态,则状态将存储在适当的版本中(请参见上面的文本)。

If you need revert just created object, you should remove it manually. 如果需要还原刚刚创建的对象,则应手动将其删除。

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

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