【问题标题】:flask python manage.py shell equivalent in fastapifastapi 中的烧瓶 python manage.py shell 等效项
【发布时间】:2021-05-01 13:39:29
【问题描述】:

我在我的烧瓶应用程序中使用 python manage.py shell 来使用 IPython shell 从 shell 访问我的应用程序。在 fastApi 中是否有任何等价物?

【问题讨论】:

    标签: ipython fastapi


    【解决方案1】:

    如果你想直接与 orm 模型交互,可以在交互模式下运行 main.py 并在那里导入我所有的模型。

    要进入交互模式,运行

    python3 -i app/app/main.py

    -i 标志可以让你

    执行脚本或命令后进入交互模式

    进入交互式shell后,可以使用导入所有模型

    from app.models import *

    请注意,您的模型结构可能不同,因此您必须相应地更改上述导入。在我的例子中,所有模型都导入到 __init__.py 文件中,这就是为什么我可以一次性导入所有模型。

    【讨论】:

      【解决方案2】:

      是的,有一种方法可以使用 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()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多