Ram Rachum
Ram Rachum

Reputation: 88578

Django: Automatically executing statements in `manage.py shell`

Every time I start a shell using python manage.py shell, I want a few lines to be executed automatically. (In my case it would be a few import lines in the style of import django, my_app.)

How do I do this?

Upvotes: 2

Views: 195

Answers (2)

Ram Rachum
Ram Rachum

Reputation: 88578

I ended up monkeypatching IPython.frontend.terminal.embed.InteractiveShellEmbed.__call__ to add the definitions I wanted. (I know many people are opposed to monkeypatching but I find it to be a good solution in this case.)

Upvotes: 0

Private
Private

Reputation: 2694

The package django-extensions does what you want. If you pip install django-extensions, and you can add the app as always (in your app list, then run syncdb), then you get a command called shell_plus.

That command loads all your models automatically if you run python manage.py shell_plus. Really handy! If you combine that the power of IPython you get a nice environment to interact with your models using the django ORM.

More information:

Hope this helps. It might be less work than writing start-up scripts. As an extra django-extensions gives you great functions like graph_models (which can give you a png drawing of your database) and show_urls.

Upvotes: 2

Related Questions