SQLite对事务机制的支持是不完整的,Django 1.6 开始对事物的支持迁移到了有数据库直接支持。

手上正好有个项目是很久以前的,部分开发调整环境还行想继续使用 SQLite ;一旦遇到有事务处理的代码就提示

Your database backend doesn't behave properly when
autocommit is off. Turn it on before using 'atomic'.

在数据库配置中添加设置均无效。最终还是用强行 hack 的方式暂时解决。希望 Django 团队能在新版本中提供配置允许 SQLite 环境忽略这个异常。方法如下:

修改文件 site-packages\django\db\transaction.py 242 行

if connection.features.autocommits_when_autocommit_is_off:
    pass
    #raise TransactionManagementError(
    #    "Your database backend doesn't behave properly when "
    #    "autocommit is off. Turn it on before using 'atomic'.")

参考链接