Django连续dumpdata调用失败,即使在单独运行时工作正常

2024-04-20 09:30:16 发布

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

我在尝试在我的站点上设置自动备份时遇到了一个问题。问题归结为以下几个方面。在

我打开pythonshell并调用dumpdata命令两次。它第一次工作,第二次返回空列表。之后,所有进一步的dumpdata调用都返回空列表:

>>> python manage.py shell
>>> from django.core.management import call_command
>>> call_command("dumpdata")
[{"pk": 1, (...) // lots of data //
>>> call_command("dumpdata")
>>> []

为了让它再次工作,我需要重新启动pythonshell。在

编辑:我使用django1.4和python2.6

编辑2:我目前的假设是,这个问题与这个问题有关:https://code.djangoproject.com/ticket/5423-5年前就确定了,根据django1.5发行说明,将在下一个版本中解决。有人知道如何在不改变机器上运行的1.4框架代码的情况下解决这个问题吗?在

Edit3:但是整个数据库的sql转储只有0.5MB,这使得序列化内存不足是很不可能的。不管怎样,在这种情况下,我不会得到一个明显的错误吗?在

编辑4:解开谜团。正如Tomasz Gandor正确判断的那样,问题是shell在一个事务中执行命令,在其中一个命令导致DBError之后,将忽略更多的DB调用,如下所述:https://code.djangoproject.com/ticket/10813。为什么第一次转储数据时数据库错误没有被明确报告,对我来说仍然是个谜。在


Tags: https命令com数据库编辑列表情况code
1条回答
网友
1楼 · 发布于 2024-04-20 09:30:16

我看到django在搞乱交易。在

我在调试器下执行了一个简单的示例:

# test.py
from django.core.management import call_command
call_command("dumpdata")
print "\n -"
call_command("dumpdata")
print

并称之为:

^{pr2}$

我的日志.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()

每次都会把整个数据库都吐出来!在

相关问题 更多 >