说我在我的Django应用程序的python shell中做了类似的事情:
>>>from myapp.models import User
>>>user = User.objects.get(pk=5)
>>>groups = user.groups.all()
我想做的是以某种方式隐藏这3个命令而不离开shell。我的目标是,如果稍后重新启动shell会话,我可以快速恢复类似的环境。
答案 0 :(得分:2)
Django shell将使用IPython(如果可用),它支持持久历史记录。
另外,编写一次性脚本is not difficult。
答案 1 :(得分:1)
非常感谢Ignacio,安装了IPython:
>>>from myapp.models import User
>>>user = User.objects.get(pk=5)
>>>groups = user.groups.all()
>>>#Ipython Tricks Follow
>>>hist #shows you lines in your history
>>>edit 1:3 # Edit n:m lines above in text editor. I save it as ~/testscript
>>>run ~/testscript
的Groovy!
答案 2 :(得分:1)
Koobz,因为你刚刚成为ipython的最新转换版,我用了一个很酷的hack用于以交互模式自动导入我的所有应用程序模型:
#!/bin/env python
# based on http://proteus-tech.com/blog/code-garden/bpython-django/
try:
from django.core.management import setup_environ
import settings
setup_environ(settings)
print "imported django settings"
try:
exec_strs = ["from %s.models import *"%apps for apps in settings.INSTALLED_APPS if apps not in ['django_extensions']]
for x in exec_strs:
try:
exec(x)
except:
print 'not imported for %s' %x
print 'imported django models'
except:
pass
except:
pass
然后我只是别名:ipython -i $HOME/.pythonrc