Alternate method of autoloading Django models in ipython

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def load_django_models():
    try:
        from django.db.models.loading import get_models
        for m in get_models():
            ip.ex("from %s import %s" % (m.__module__, m.__name__))
    except ImportError:
        print "INFO: could not find a django env"

...

def main():
    ...
    load_django_models()

Comments

jpt (on January 16, 2008):

great snippet, I use ipython for other things (virtually everything actually) so I changed the code to

def load_django_models():
    try:
        from django.db.models.loading import get_models
        for m in get_models():
            ip.ex("from %s import %s" % (m.__module__, m.__name__))
        print 'INFO: Loaded Django models.'
    except ImportError:
        pass

but other than that, terrific

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.