Executing a Django Shell Command from the Command Line

26.9k views Asked by At

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

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

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?

2

There are 2 answers

1
Ignacio Vazquez-Abrams On BEST ANSWER

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

4
kraiz On

Pipe it ;)

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