如何撤消Django数据库中的最后一个事务

2024-05-15 00:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我在csv文件中有一个数据要插入数据库。现在我不能保证提供的数据会符合我的需要,所以我想如果有任何异常,那么以前在数据库上的事务应该是undo。在

我目前所做的是保存数据,直到发现异常为止。我抛出异常,然后在行号处打印异常。我想要的是整个文件应该被再次插入,没有任何重复的行。我该怎么做?在

这是我的代码:-在

for row in rows:
    line += 1
    try : 
        obj = XYZ(col1 = row[0],col2=row[1])
        obj.save()
    except : raise ("Excetion found at line ", line)

我应该如何撤消在异常发生之前完成的所有事务??在


Tags: 文件csv数据代码in数据库objfor
1条回答
网友
1楼 · 发布于 2024-05-15 00:34:02

听起来你要找的是^{},引用文档(emphasis mine):

Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

相关问题 更多 >

    热门问题