事务
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.....