从命令行执行Django Shell命令

时间:2023-01-24 12:14:51

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的等效-c选项。有没有办法做到这一点?

2 个解决方案

#1


6  

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

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

#2


34  

Pipe it ;)

管它;)

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

#1


6  

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

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

#2


34  

Pipe it ;)

管它;)

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