I am in anguish over watching my colleagues fire up python manage.py
shell...
...and then typing several incredibly long import lines before
actually getting anything done.
These same colleagues also seem to have itchy ^C fingers, meaning they
bail out of the shell too often, too.
How can we fix the first problem? How can this (otherwise useful)
shell use a minimal or invisible import rubric, to grab, say, a bunch
of models so they are all ready & available for use?
--
Phlip
http://penbird.deviantart.com/
I use something like this (assuming you have iPython installed):
**********
#!/usr/bin/env ipython -i -nobanner
# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOUR.SETTINGS.MODULE'
from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache
local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os
**********
Save that to a file in your PATH, make it executable, and you should be set.
> #!/usr/bin/env ipython -i -nobanner
> Save that to a file in your PATH, make it executable, and you should be set.
Awesome--thanks. And, instead of chmod +x-ing it, I added it to our
common fab files, so everyone gets it with 'fab shell'.
And I could not get ip = IPython.ipapi.get() working (not sure why -
it returned a None), so I added it with this contemptible but
bulletproof hack:
def shell():
open('.ipython', 'w').write('''
# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings' # TODO look
up which one
from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache
local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os
''')
os.system('ipython -i -nobanner .ipython')
--
Phlip
http://c2.com/cgi/wiki?ZeekLand
My local copy actually has the following code in it:
********************
import os
import re
import sys
m = re.search(r'^([a-zA-Z0-9_]+)-shell$', os.path.basename(sys.argv[0]))
mn = 'avclub'
if m:
g = m.group(1)
if g in ['avclub', 'onion']:
mn = g
del g
del m
os.environ['DJANGO_SETTINGS_MODULE'] = 'afns.sites.local_%s.settings'
% (mn,) # "afns" is the Onion's Python package
********************
This way I can just make symlinks named "avclub-shell" and
"onion-shell" to the original executable, and it will use the
respective settings files automatically without having to maintain
separate copies. You could also get fancier with optparse, I suppose.
[1] http://github.com/django-extensions/django-extensions
On Mar 26, 11:52 pm, "Tom X. Tobin" <tomxto...@tomxtobin.com> wrote:
Yes. shell_plus is quite handy.
$ bin/manage.py shell_plus
[...]
From 'api' autoload: RequestLog
From 'lostiempos' autoload: Section, Subsection, Ad, HtmlArchive,
SearchHistory, SearchHistoryCount
From 'south' autoload: MigrationHistory
Imports all your models by default. Also with iPython you can write
"from" then press [Up]
and will show you the entries starting with "from" in the history.
Regards,
Rolando
I wrote a similar app called django-bshell which does the same, but
uses bpython as the shell.
You can find this on pypi, and it adds a 'bshell' manage command.
Matt.