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="余额")
models.py
from django.contrib import admin
from django.urls import path,re_path,include
from app01 import views

urlpatterns = [
    re_path("index.html",views.index)
]
urls.py

 

 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")
views.py

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")
views.py

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")
views.py

 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")
views.py

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")
触发错误,raise ValueError,未出现func的东西

 

posted @ 2018-08-22 23:07  fat39  阅读(232)  评论(0编辑  收藏  举报