Python 从命令行执行 Django Shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4797671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:34:18  来源:igfitidea点击:

Executing a Django Shell Command from the Command Line

pythondjangodjango-shell

提问by Cerin

I would like to execute a command via Django's manage.py shell function solely from the command line

我想仅从命令行通过 Django 的 manage.py shell 函数执行命令

e.g.

例如

manage.py shell -c "from myapp import models; print models.MyModel.some_calculation()"

the came way you might use the -c option with the normal Python interpreter

您可以将 -c 选项与普通 Python 解释器一起使用的方式

e.g.

例如

python -c "print 'hello world'"

However, I don't see an equivalent -c option for manage.py shell. Is there a way to do this?

但是,我没有看到manage.py shell. 有没有办法做到这一点?

采纳答案by Ignacio Vazquez-Abrams

Not like that. But it is easy enough to write a standalone script for Django.

不是这样的。但是为 Django编写一个独立的脚本是很容易的。

回答by kraiz

Pipe it ;)

管它;)

echo "print 'hello world'" | python manage.py shell