在shell_plus
中,有没有办法像模型一样自动导入选定的帮助器方法?
我经常打开 shell 输入:
proj = Project.objects.get(project_id="asdf")
我想替换为:
proj = getproj("asdf")
最佳答案
Found it in the docs。从那里引用:
Additional Imports
In addition to importing the models you can specify other items to import by default. These are specified in
SHELL_PLUS_PRE_IMPORTS
andSHELL_PLUS_POST_IMPORTS
. The former is imported before any other imports (such as the default models import) and the latter is imported after any other imports. Both have similar syntax. So in your settings.py file:SHELL_PLUS_PRE_IMPORTS = ( ('module.submodule1', ('class1', 'function2')), ('module.submodule2', 'function3'), ('module.submodule3', '*'), 'module.submodule4' )
The above example would directly translate to the following python code which would be executed before the automatic imports:
from module.submodule1 import class1, function2 from module.submodule2 import function3 from module.submodule3 import * import module.submodule4
These symbols will be available as soon as the shell starts.
关于django - 将方法导入添加到shell_plus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19236771/