Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add method imports to shell_plus

In shell_plus, is there a way to automatically import selected helper methods, like the models are?

I often open the shell to type:

proj = Project.objects.get(project_id="asdf")

I want to replace that with:

proj = getproj("asdf")
like image 553
Brett Thomas Avatar asked Oct 07 '13 23:10

Brett Thomas


1 Answers

Found it in the docs. Quoted from there:

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.

like image 161
Brett Thomas Avatar answered Nov 07 '22 20:11

Brett Thomas