【发布时间】:2021-05-01 13:39:29
【问题描述】:
我在我的烧瓶应用程序中使用 python manage.py shell 来使用 IPython shell 从 shell 访问我的应用程序。在 fastApi 中是否有任何等价物?
【问题讨论】:
我在我的烧瓶应用程序中使用 python manage.py shell 来使用 IPython shell 从 shell 访问我的应用程序。在 fastApi 中是否有任何等价物?
【问题讨论】:
如果你想直接与 orm 模型交互,可以在交互模式下运行 main.py 并在那里导入我所有的模型。
要进入交互模式,运行
python3 -i app/app/main.py
-i 标志可以让你
执行脚本或命令后进入交互模式
进入交互式shell后,可以使用导入所有模型
from app.models import *
请注意,您的模型结构可能不同,因此您必须相应地更改上述导入。在我的例子中,所有模型都导入到 __init__.py 文件中,这就是为什么我可以一次性导入所有模型。
【讨论】:
是的,有一种方法可以使用 IPython package 运行 shell
就个人而言,我制作了一个 manage.py 文件(下面的示例)并将其用作
python manage.py shell
from IPython import embed
from main import app # where your fastAPI app is located, you can initialize it here
@app.command()
def runserver():
app()
@app.command()
def shell():
embed()
【讨论】:
AttributeError: 'FastAPI' object has no attribute 'command'@app.command()