Login

Autoload Django Models When Using ./manage.py shell

Author:
sheats
Posted:
January 9, 2008
Language:
Python
Version:
.96
Tags:
import shell interpreter autoload
Score:
7 (after 9 ratings)

It's a pain to import all the Django models you want to use in the Python shell every time you start it. Here's how you can get IPython to autoload all your Django models for you every time you start the shell using ./manage.py shell.

Put the code in a .py file in the root of your project. Then tell IPython to load the script in ~/.ipython/ipythonrc in the "Python files to load and execute" section.

1
2
3
from django.db.models.loading import get_models
for m in get_models():
    exec "from %s import %s" % (m.__module__, m.__name__)

More like this

  1. Simple Plone Migration by msm-art 7 years, 1 month ago
  2. Quick startup using manage.py shell by dnordberg 7 years, 6 months ago
  3. cron/console bootstrap django by nstrite 7 years, 6 months ago
  4. PyCrust Shell by robvdl 7 years, 4 months ago
  5. Alternate method of autoloading Django models in ipython by drewr 7 years, 1 month ago

Comments

udfalkso (on January 10, 2008):

I've been dying for a solution to this, thanks!

#

Please login first before commenting.