I want to undo my last POST/PUt/DELETE or any sort of transaction within a database. I read about Django-reversion & tried implementing it. 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, butdjango-reversion
allows you to roll back to the state of any previous revision. So modify yourShipmentScanMapping
object and try again.Update
Initialize data:
Update data:
Revert:
Update2
You can also recover a deleted object, see docs.
get_unique_for_object
method returns a list of all previous versions, latest versions first. So you can select as usual selection from the list in python. By the way you can find the most recent version for a given date:Update3
Okay, let's consider how it works. When you register your model, the
django-reversion
begins to watchpost_save
signals from the model. 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 areversion.models.Version
model.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. 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.