Created
August 5, 2011 09:41
-
-
Save vladimiroff/1127214 to your computer and use it in GitHub Desktop.
Try to import django environment on each bpython2 call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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