Django with bpython shell on Mac OSX/Linux

  1. Add this line to your .profile:

    export PYTHONSTARTUP=~/.pythonrc.py
    
  2. Download .pythonrc.py and copy it to your home directory:

    #.pythonrc
    try:
        from django.core.management import setup_environ
        import settings
        setup_environ(settings)
    
        from django.db.models.loading import get_models, get_apps
    
        for app in get_apps():
            app_models = get_models(app)
            if not app_models:
                continue
            model_labels = ", ".join([model.__name__ for model in app_models])        
            try:
                exec("from %s import *" % app.__name__)
                print "From '%s' autoload: %s" % (app.__name__.split('.')[-2], model_labels)
            except:
                print "Not imported for '%s'" % app.__name__.split('.')[-2]
    except:
        pass
    
  3. Close current terminal and open a new one.

  4. Change to your django project directory an start bpython. If everthing went fine you should see a list of your imported models:

    ...
    From 'auth' autoload: Permission, Group, User, Message
    From 'contenttypes' autoload: ContentType
    From 'sessions' autoload: Session
    From 'sites' autoload: Site
    ...