以编程方式调用call_command('dumpdata')后忽略语言设置

时间:2011-08-22 14:40:21

标签: django django-views

我目前正在以编程方式调用dumpdata从我的django应用程序中导出数据。

from django.core.management import call_command
# and various other imports not directly relevant

response = HttpResponse(mimetype='application/json', )
response['Content-Disposition'] = "filename=%s" % backup_name
sys.stdout = response
call_command('dumpdata')

导出效果很好(如果有点慢)但之后设置.LANGUAGE_CODE被忽略,所有页面都恢复为原始语言英语。任何想法为什么会发生这种情况?

我正在使用以下内容: - Python 2.7 - Django 1.3 - 用于管理语言翻译的Rosetta

1 个答案:

答案 0 :(得分:2)

这可能是由django.core.management.base.BaseCommand对象中的代码引起的。代码中的解释是:

# Switch to English, because django-admin.py creates database content
# like permissions, and those shouldn't contain any translations.
# But only do this if we can assume we have a working settings file,
# because django.utils.translation requires settings.

执行实际语言切换的代码是:

from django.utils import translation
translation.activate('en-us')