我想撤消上一次 POST/PUt/DELETE 或数据库中的任何类型的事务。我阅读了Django-reversion并尝试实现它。但没有运气。这是我的代码。请确认我做错了什么!!!
要反转的模型:-
@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)
意见
@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")
我在做什么错??