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?
-
Please don't. Please write a two-line script file.S.Lott– S.Lott2011年01月25日 19:05:11 +00:00Commented Jan 25, 2011 at 19:05
-
1see custom admin commandsvirhilo– virhilo2011年01月25日 19:05:14 +00:00Commented Jan 25, 2011 at 19:05
3 Answers 3
Pipe it ;)
echo "print('hello world')" | python manage.py shell
4 Comments
cat my_script.py | python manage.py shellecho "$(cat my_script.py)" | python manage.py shell instead.... | python manage.py shell -i python\n at the end (with echo -e if necessary).Not like that. But it is easy enough to write a standalone script for Django.
1 Comment
For anyone new coming to this question: Since Django 1.10, the -c/--command is there, and it works exactly like you'd expect. From the official docs:
Lets you pass a command as a string to execute it as Django, like so:
django-admin shell --command="import django; print(django.__version__)"