An error occurred in the current transaction. You can‘t execute queries until the end of the ‘atomic

在使用Django时遇到一个错误:'An error occurred in the current transaction. You can't execute queries until the end of the 'atomic block'。问题源于在原子事务中尝试执行查询。分析表明,不建议在'with transaction.atomic'中捕获异常,并且这样做可能会导致TransactionManagementError。解决方案在于正确处理事务和异常。
摘要由CSDN通过智能技术生成

问题

如图所示,错误返回结果是An error occurred in the current transaction. You can't execute queries until the end of the 'atomic block

 (这个在django中,用swagger进行调试,自己捕获异常的结果)

分析

字面意思

就是django中从开启事务后,到事务with结束(包含异常)后,执行了数据查询语句。

我代码的基本结构(犯了最底下的两个要点)

class InfoStore(APIView):

    @swagger_auto_schema(******)
    def post(self, request):
        receive_data = request.data

        with transaction.atomic():   # 1 在transaction中去try except自己捕获异常
            # 创建事务保存点
            save_id = transaction.savepoint()
            try:
                if(InfoTest.objects.filter(**
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值