flask-sqlalchemy或sqlalchemy [英] flask-sqlalchemy or sqlalchemy

查看:118
本文介绍了flask-sqlalchemy或sqlalchemy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在flask和sqlalchemy中都是新手,我只是开始在flask应用程序上工作,现在我正在使用sqlalchemy.我想知道使用flask-sqlalchemy与sqlalchemy是否可以获得任何显着的好处.我在 http://packages.python.org/Flask-SQLAlchemy/index.html也许我不明白它的价值!感谢您的澄清.

Flask-SQLAlchemy的主要功能是与Flask应用程序正确集成-它创建并配置引擎,连接和会话,并将其配置为与Flask应用程序一起使用

此设置非常复杂,因为我们需要创建作用域会话并根据Flask应用程序请求/响应生命周期正确处理它.

在理想世界中,这将是Flask-SQLAlchemy的唯一功能,但实际上它增加了一些其他内容.这是一篇很好的博客文章,其中概述了它们:解密Flask-SQLAlchemy . /p>

当我第一次使用Flask和SQLAlchemy时,我不喜欢这种开销.我过去了,从扩展中提取了会话管理代码.尽管我发现正确进行此集成非常困难,但这种方法行之有效.

因此,更简单的方法(在我正在研究的另一个项目中使用)是将Flask-SQLAlchemy放进去,而不使用它提供的任何其他功能.您将拥有db.session,您可以像使用纯SQLAlchemy设置一样使用它.

I am new in both flask and sqlalchemy, I just start working on a flask app, and I am using sqlalchemy for now. I was wondering if there is any significant benefit I can get from using flask-sqlalchemy vs sqlalchemy. I could not find enough motivations in http://packages.python.org/Flask-SQLAlchemy/index.html or maybe I did not understand the value!! I would appreciate your clarifications.

解决方案

The main feature of the Flask-SQLAlchemy is proper integration with Flask application - it creates and configures engine, connection and session and configures it to work with the Flask app.

This setup is quite complex as we need to create the scoped session and properly handle it according to the Flask application request/response life-cycle.

In the ideal world that would be the only feature of Flask-SQLAlchemy, but actually it adds few more things. Here is a good blog post with the overview of them: Demystifying Flask-SQLAlchemy.

When I first worked with Flask and SQLAlchemy, I didn't like this overhead . I went over and extracted the session management code from the extension. This approach works, although I discovered that it is quite difficult to do this integration properly.

So the easier approach (which is used in another project I am working on) is to just drop the Flask-SQLAlchemy in and don't use any of additional features it provides. You will have the db.session and you can use it as if it was pure SQLAlchemy setup.

这篇关于flask-sqlalchemy或sqlalchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆