【问题标题】:How can I perform Django's `syncdb --noinput` with call_command?如何使用 call_command 执行 Django 的 `syncdb --noinput`?
【发布时间】:2011-11-08 23:40:46
【问题描述】:
>>> 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。

【问题讨论】:

  • 已解决:call_command('syncdb', interactive = False)
  • this question 的重复?
  • @lbp 您不应该在评论中插入解决方案,而是在答案中然后接受它。否则这个问题将永远没有公认的答案。

标签: django django-admin syncdb


【解决方案1】:
call_command('syncdb', interactive = False)

编辑:

我在源代码中找到了答案。所有管理命令的源代码都可以在名为management/commands/(command_name).py的python模块中找到

syncdb命令所在的python模块是django.core.management.commands.syncdb

要查找命令的源代码,您可以执行以下操作:

(env)$ ./manage.py shell
>>> from django.core.management.commands import syncdb
>>> syncdb.__file__
'/home/user/env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.pyc'
>>> 

当然,检查syncdb.py的内容,而不是syncdb.pyc。

或查看online sourcesyncdb.py 脚本包含:

    make_option('--noinput', action='store_false', dest='interactive', default=True,
        help='Tells Django to NOT prompt the user for input of any kind.'),

这告诉我们,如果我们想使用 call_command 函数自动执行命令,我们应该使用 interactive 而不是命令行上的 --noinput

【讨论】:

  • 有谁知道你为什么用interactive而不是noinput?这是在 Django 文档中的任何地方吗?
  • 据我所知,关于这个主题的文档非常模糊。这里有一个例子:docs.djangoproject.com/en/dev/ref/django-admin/…
  • 非常感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2017-05-08
  • 2012-04-30
  • 2015-02-16
  • 2013-02-17
相关资源
最近更新 更多