如何使用 call_command 执行 Django 的 `syncdb --noinput` ?
>>> from django.core.management import call_command
>>> call_command('syncdb')
从 python 脚本中执行syncdb管理命令。但是,我想
$ python manage.py syncdb --noinput
在 python shell 或脚本中运行等效的命令。我怎样才能做到这一点?
以下几行不会在不询问我是否要创建超级用户的情况下打断我。
>>> call_command('syncdb', noinput = True) # asks for input
>>> call_command('syncdb', 'noinput') # raises an exception
我使用 Django 1.3。
>>> from django.core.management import call_command
>>> call_command('syncdb')
executes the syncdb management command from within a python script. However, I want to run the equivalent of
$ python manage.py syncdb --noinput
from within a python shell or script. How can I do that?
The following lines don't work without interrupting me with the question whether I want to create a super user.
>>> call_command('syncdb', noinput = True) # asks for input
>>> call_command('syncdb', 'noinput') # raises an exception
I use Django 1.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
我在源代码中找到了答案。所有管理命令的源代码都可以在名为
management/commands/(command_name).py
的 python 模块中找到syncdb
命令所在的 python 模块是django.core.management.commands.syncdb
要查找该命令的源代码,您可以执行以下操作:
当然,检查syncdb.py 的内容,而不是syncdb.pyc 的内容。
或者查看在线源,
syncdb.py
脚本包含:它告诉我们,如果我们不使用命令行上的
--noinput
,我们应该使用interactive
想要自动化命令call_command
函数。EDIT:
I found the answer in the source code. The source code for all management commands can be found in a python module called
management/commands/(command_name).py
The python module where the
syncdb
command resides isdjango.core.management.commands.syncdb
To find the source code of the command you can do something like this:
Of course, check the contents of syncdb.py, and not syncdb.pyc.
Or looking at the online source, the
syncdb.py
script contains:that tells us that instead of
--noinput
on the command line, we should useinteractive
if we want to automate commands with thecall_command
function.