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

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

问题描述

我是flask 和sqlalchemy 的新手,我刚刚开始开发flask 应用程序,现在我正在使用sqlalchemy.我想知道使用 Flask-sqlalchemy 与 sqlalchemy 是否有任何显着的好处.我在 http://packages.python.org/Flask-SQLAlchemy/index.html 或者我不明白这个值!!感谢您的澄清.

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.

推荐答案

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

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.

这个设置非常复杂,因为我们需要创建范围会话 并根据 Flask 应用程序请求/响应生命周期妥善处理.

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.

在理想的世界中,这将是 Flask-SQLAlchemy 的唯一功能,但实际上,它增加了更多的东西.查看文档了解更多信息.或查看此博客文章及其概述:Demystifying Flask-SQLAlchemy(更新:原始文章目前不可用,有一个 网络存档上的快照).

In the ideal world that would be the only feature of Flask-SQLAlchemy, but actually, it adds few more things. Check out the docs for more info. Or see this blog post with the overview of them: Demystifying Flask-SQLAlchemy (update: the original article is not available at the moment, there is a snapshot on webarchive).

当我第一次使用 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.

因此,更简单的方法(在我正在从事的另一个项目中使用)是将 Flask-SQLAlchemy 放入并且不要使用它提供的任何附加功能.您将拥有 db.session 并且可以像使用纯 SQLAlchemy 设置一样使用它.

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.

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

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