事务

a. 事例一:

try:
    from django.db import transaction
    with transaction.atomic():
        models.UpDown.objects.create(user_id=user_id,article_id=article_id,up=False)
        models.Article.objects.filter(nid=article_id).update(down_count=F('down_count')+1)
except Exception as e:
    response['status'] = False
    response['msg'] = str(e)

b. 事例二:

#函数里面有数据库操作,加在函数上
from django.db.transaction import atomic

@atomic
def cmd(self):
	model.....
	model.....

 

  

  

 

posted @ 2017-08-16 22:08  golangav  阅读(248)  评论(0编辑  收藏  举报