Cerin
Cerin

Reputation: 64689

Executing a Django Shell Command from the Command Line

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?

Upvotes: 20

Views: 27381

Answers (3)

Nikita Karamov
Nikita Karamov

Reputation: 884

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__)"

Upvotes: 0

kraiz
kraiz

Reputation: 2218

Pipe it ;)

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

Upvotes: 46

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798446

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

Upvotes: 6

Related Questions