【问题标题】:Django manage.py --no-input . Yes or no?Django manage.py --no-input 。是还是不是?
【发布时间】:2017-06-16 00:59:13
【问题描述】:
我在文档中找不到这个。当我运行python manage.py collecstatic --no-input 时,这是否意味着它会对在此过程中弹出的任何提示回答“是”? python manage.py migrate --no-input 也是如此。
【问题讨论】:
标签:
django
django-manage.py
【解决方案1】:
对于收集静态:
message.append(
'Are you sure you want to do this?\n\n'
"Type 'yes' to continue, or 'no' to cancel: "
)
if self.interactive and input(''.join(message)) != 'yes':
raise CommandError("Collecting static files cancelled.")
所以对于收集静态,如果您设置--no-input,它会将interactive 设置为False,并且如您在上面看到的,将为您回答问题yes。
对于迁移来说,由于 django 信号,它要复杂得多。 migrate 管理本身不问任何问题,但其他已安装的应用程序可能会挂接到 pre_migrate_signal 或 post_migrate_signal 并以自己的方式处理交互性。我知道的最常见的是contenttypes
对于contenttypes,--no-input 回答“否”,如“否,请不要删除任何过时的内容类型”:
if interactive:
content_type_display = '\n'.join(
' %s | %s' % (ct.app_label, ct.model)
for ct in to_remove
)
ok_to_delete = input("""The following content types are stale and need to be deleted:
%s
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
else:
ok_to_delete = False