Skip to content

Instantly share code, notes, and snippets.

@kols
Created September 14, 2012 06:37
Show Gist options
  • Save kols/3720222 to your computer and use it in GitHub Desktop.
Save kols/3720222 to your computer and use it in GitHub Desktop.
ipython django extension
"""If we're working with a Django project, set up the environment
"""
import os
def load_ipython_extension(ipython):
try:
import django
except ImportError:
return
settings_module = 'settings'
if django.VERSION[0] >= 1 and django.VERSION[1] >= 4:
# New settings module position for Django >= 1.4
settings_module = \
os.path.basename(os.path.abspath(os.curdir)) + '.' + 'settings'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
from django.test.client import Client
from django.conf import settings
from django.db.models.loading import get_models
class DjangoModels(object):
"""Loop through all the models in INSTALLED_APPS and import them.
"""
def __init__(self):
for m in get_models():
setattr(self, m.__name__, m)
print "Django Environment -> %s\n" % \
os.environ['DJANGO_SETTINGS_MODULE']
ipython.push({
'D': django,
'M': DjangoModels(),
'C': Client(),
'S': settings,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment