Django_事务
介绍
函数说明
from django.db import transaction
transaction.atomic # 原子性操作,把一系列操作当做一个整体,错了则集体回退 transaction.on_commit(func,using = None) # 在整个事件完成后,执行func函数
使用方法
from django.db import transaction
# atomic @transaction.atomic def index(request): ... def index(request): with transation.atomic(): .... .... # on_commit @transaction.atomic def index(request): do_something... transation.oncommit(func)
示例
from django.db import models class Account(models.Model): nid = models.IntegerField(primary_key=True) money = models.IntegerField(verbose_name="余额")
from django.contrib import admin from django.urls import path,re_path,include from app01 import views urlpatterns = [ re_path("index.html",views.index) ]
1、正常情况,转账成功
from django.shortcuts import render,HttpResponse from app01 import models from django.db.models import F def index(request): models.Account.objects.filter(nid=1).update(money=F("money")-1) # 账号1给账号2转账1元钱 # raise IndexError models.Account.objects.filter(nid=2).update(money=F("money")+1) return HttpResponse("OK")
2、转账到一半出现错误
from django.shortcuts import render,HttpResponse from app01 import models from django.db.models import F def index(request): models.Account.objects.filter(nid=1).update(money=F("money")-1) raise IndexError models.Account.objects.filter(nid=2).update(money=F("money")+1) return HttpResponse("OK")
3、还原所有money到1000,配置事务
from django.shortcuts import render,HttpResponse from app01 import models from django.db import transaction from django.db.models import F def index(request): with transaction.atomic(): models.Account.objects.filter(nid=1).update(money=F("money")-1) raise IndexError models.Account.objects.filter(nid=2).update(money=F("money")+1) return HttpResponse("OK") # 可以使用装饰器 # @transaction.atomic # def index(request): # models.Account.objects.filter(nid=1).update(money=F("money")-1) # raise ValueError("出错") # models.Account.objects.filter(nid=2).update(money=F("money")+1) # return HttpResponse("OK")
4、验证on_commit
说明:如果原子性操作出错,则整个on_commit都不会执行,无论on_commit放在什么位置。这与直接在该位置执行func是不一样的。
from django.shortcuts import render,HttpResponse from app01 import models from django.db import transaction from django.db.models import F def before(): print("before") # 可以做一些特定工作,如发邮件、记录log def after(): print("after") @transaction.atomic def index(request): transaction.on_commit(before) models.Account.objects.filter(nid=1).update(money=F("money")-1) # raise ValueError("出错") models.Account.objects.filter(nid=2).update(money=F("money")+1) transaction.on_commit(after) return HttpResponse("OK")
from django.shortcuts import render,HttpResponse from app01 import models from django.db import transaction from django.db.models import F def before(): print("before") # 可以做一些特定工作,如发邮件、记录log def after(): print("after") @transaction.atomic def index(request): transaction.on_commit(before) models.Account.objects.filter(nid=1).update(money=F("money")-1) raise ValueError("出错") models.Account.objects.filter(nid=2).update(money=F("money")+1) transaction.on_commit(after) return HttpResponse("OK")