【Django】Django Shell

Posted by 西维蜀黍 on 2019-10-19, Last Modified on 2021-09-21

Django shell

$ tree
.
├── core
│   └── models.py
│ 	...
└── manage.py
...
1 directory, 2 files

打开你本地的终端(不是在Python解析器里面) 然后输入这个命令:

$ python manage.py shell
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from .core import models
>>> models.Channel.objects.all()
<QuerySet [<Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>, <Channel: Channel object>]>

类似的,这里也是通过 from xxx import xxx 语句引用一个模块,所以可以直接调用 models 模块的方法,即通过 [方法名](),而方法名前不再需要声明该方法所属的模块。

Reference