Skip to content

Instantly share code, notes, and snippets.

@vladimiroff
Created August 5, 2011 09:41
Show Gist options
  • Save vladimiroff/1127214 to your computer and use it in GitHub Desktop.
Save vladimiroff/1127214 to your computer and use it in GitHub Desktop.
Try to import django environment on each bpython2 call.
'''
Try to import django environment on each bpython2 call.
So if there is a Django project in current directory we could use
bpython instead of ipython with the `manage.py shell_plus` command,
or just with `bpython2`
TODO: Optimize this only for bpython calls.
'''
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 {0} import *".format(app.__name__))
print("From '{0}' autoload: {1}".format(app.__name__.split('.')[-2], model_labels))
except:
print("Not imported for '{0}'".format(app.__name__.split('.')[-2]))
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment