django - 将方法导入添加到shell_plus

标签 django django-extensions

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 and SHELL_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/

相关文章:

python - Django 扩展 - dumpscript 无法正常工作

python - 如何解决在 Linux Mint 18.1 中找不到 Django 'setup.py'?

python - 使用混合 Django 模型和字典的 Django JSON 序列化

python - 如何使用 kCacheGrind 打开 python 配置文件数据?

django - 使用 AutoSlugField 填充现有模型对象

python - django-extensions shell_plus --kernel 指定连接文件

python - 如何显示 mySQL 中两个不同表的值?

javascript - Gentelella主题中的蓝色页脚

python - DRF : Simple foreign key assignment with nested serializers?

python - 如何安装 django-utils?