我看到 django 正在处理事务。
我在调试器下执行了一个简单的例子:
# test.py
from django.core.management import call_command
call_command("dumpdata")
print "\n---"
call_command("dumpdata")
print
并称它为:
DJANGO_SETTINGS_MODULE=settings python test.py > log.txt
我的 log.txt 以“---\n[]\n”结尾
在我发现的调试器中运行它之后
django.core.management.commands.dumpdata.handle()
,model.objects.all() 的深处一直返回 []。
我调用了model.objects.iterator(),得到了异常:
(Pdb) list(model.objects.iterator())
*** Error in argument: '(model.objects.iterator())'
(Pdb) p list(model.objects.iterator())
*** DatabaseError: DatabaseError('current transaction is aborted, commands ignored until end of transaction block\n',)
(Pdb)
因此,我编写了与交易本身有关的代码:
# test.py version 2.0!
#!/usr/bin/env python
# from django.core.management import call_command
import django.core.management as mgmt
from django.db import transaction
'''
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
'''
@transaction.commit_manually
def main():
# mgmt.call_command('dumpdata', use_base_manager=True)
mgmt.call_command('dumpdata')
transaction.rollback()
print
print '---'
print
"""
mgmt._commands = None
import sys
reload(sys.modules['django.core.management.commands.dumpdata'])
"""
mgmt.call_command('dumpdata')
transaction.rollback()
print
if __name__ == "__main__":
main()
这会吐出整个数据库 - 每次!