简体   繁体   English

从 Flask 迁移到 FastAPI

[英]Migrating from Flask to FastAPI

I'm developing two applications in FastAPI, which would use same data model structures - they both operate over same database but under different users with different credentials information.我正在 FastAPI 中开发两个应用程序,它们将使用相同的数据 model 结构 - 它们都在相同的数据库上运行,但在具有不同凭据信息的不同用户下运行。 Therefore in my project structure, I created three folders - one for FastAPI project application A(python backend for frontend application), one for FastAPI project application B(python backend for backoffice users application) and one for common classes including files with SQLAlchemy tables definition - folder C.因此,在我的项目结构中,我创建了三个文件夹 - 一个用于 FastAPI 项目应用程序 A(python 后端用于前端应用程序),一个用于 FastAPI 项目应用程序 B(python 后端用于后台用户应用程序),一个用于通用类,包括具有 SQLAlchemy 表定义的文件- 文件夹 C。 In flask, everything was working like a charm - each from application A and B had separate config files with different credentials for database.在 flask 中,一切都像魅力一样工作 - 来自应用程序 A 和 B 的每个都有单独的配置文件,具有不同的数据库凭据。 I didn't have any problems with correct connection to database.正确连接到数据库没有任何问题。 Based on what project I started, correct database credentials were used.根据我开始的项目,使用了正确的数据库凭据。 I used flask_sqlalchemy extension and it took care of everything.我使用了 flask_sqlalchemy 扩展,它处理了一切。 Now, I'm having Settings file for each application A and B, which contain correct credentials and database connection string.现在,我拥有每个应用程序 A 和 B 的设置文件,其中包含正确的凭据和数据库连接字符串。 Problem is, that I want to have code in folder C independent on folders A and B. That means that I would like to somehow write without dependency on project A or project B SQLAlchemy files with description of common database structures.问题是,我希望文件夹 C 中的代码独立于文件夹 A 和 B。这意味着我想以某种方式编写而不依赖于项目 A 或项目 B SQLAlchemy 文件,并描述通用数据库结构。 I have no idea currently whether is this feasible or how to do it.我目前不知道这是否可行或如何去做。 I think that this kind of code and application separation is good and maybe even necessary idea in terms of security - I'm using users with different sets of permissions for each application.我认为这种代码和应用程序分离是好的,甚至在安全性方面可能是必要的想法——我为每个应用程序使用具有不同权限集的用户。 Just have no idea how to somehow correctly provide dependency of code in folder C to correct database credentials.只是不知道如何以某种方式正确地提供文件夹 C 中的代码依赖性以更正数据库凭据。

I found additional package designed specifically for use of SQL alchemy with FastAPI which solves my problem: https://pypi.org/project/FastAPI-SQLAlchemy/我发现了额外的 package 专为使用 SQL 炼金术和 FastAPI 而设计,它解决了我的问题: https:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM