[英]Where do I put and how to use Table() calls in a project with SQLAlchemy Core and flask?
So I want to use SQLAlchemy Core and flask in an application.所以我想在应用程序中使用 SQLAlchemy Core 和 Flask。 I have read through the documentation and a lot of tutorials how to structure your application.
我已经通读了文档和许多如何构建应用程序的教程。 All of the SQLAlchemy Core tutorials put the Database description directly into the app.py file of the application.
所有 SQLAlchemy Core 教程都将数据库描述直接放入应用程序的 app.py 文件中。 I'd like to put this Database description into its own python module (called tables/ here) and usw it in my views, forms etc. So I want to achieve a basic layout like the following:
我想把这个数据库描述放到它自己的 python 模块中(这里称为表/),并在我的视图、表单等中使用它。所以我想实现一个基本的布局,如下所示:
myapp/
run.py
tables/
__init__.py
tables1.py
tables2.py
...
views/
views1.py
...
...
What I struggle especially with is the following:我特别挣扎的是以下几点:
metadata = MetaData()
into tables/__init__.py
and import it in the tables/__init__.py
files?metadata = MetaData()
放入tables/__init__.py
并将其导入tables/__init__.py
文件吗?tables/tablesX.py
files if I want to access my Tables?tables/tablesX.py
文件中导入实例?table = meta.tables[table_name]
.table = meta.tables[table_name]
将表从元数据table = meta.tables[table_name]
。 But I have no clue how to pass the metadata object around so that it holds all the tables.table = Table(name, tmp_metadata,...)
directly in my views.py files?table = Table(name, tmp_metadata,...)
? Thank you for your time and best wishes,感谢您的时间和最美好的祝愿,
AH啊
Let's do it one step at a time.让我们一步一步来。 Minimal application:
最小应用:
from sqlalchemy import ...
from flask import ...
from flask.ext.sqlalchemy import ...
app = Flask()
db = SQLAlchemy()
class Foo(db.Model):
...
@app.route(...)
def foo():
Foo.query.filter(...)
return ...
Let's move the db stuff out into a separate module:让我们将 db 的东西移到一个单独的模块中:
# db.py
from sqlalchemy import ...
from flask.ext.sqlalchemy import ...
from .app import app
db = SQLAlchemy(app)
class Foo(db.Model):
...
# app.py
from flask import ...
from .db import Foo
app = Flask()
@app.route(...)
def foo():
Foo.query.filter(...)
return ...
But wait, we have a circular dependency, so we have to move app
out:但是等等,我们有一个循环依赖,所以我们必须将
app
移出:
# app.py
from flask import *
app = Flask()
# db.py
from sqlalchemy import ...
from flask.ext.sqlalchemy import ...
from .app import app
db = SQLAlchemy(app)
class Foo(db.Model):
...
# views.py
from flask import ...
from .db import Foo
from .app import app
@app.route(...)
def foo():
Foo.query.filter(...)
return ...
Next, we can split db.py
:接下来,我们可以拆分
db.py
:
# app.py same
# views.py same
# db/__init__.py
from flask.ext.sqlalchemy import ...
from ..app import app
from .foo import Foo
db = SQLAlchemy(app)
# db/foo.py
from sqlalchemy import ...
from . import db
class Foo(db.Model):
...
But wait, we have a circular dependency again, so we have to move db
out:但是等等,我们又有了一个循环依赖,所以我们必须将
db
移出:
# app.py same
# views.py same
# db/__init__.py
from .common import db
from .foo import Foo
# db/common.py
from flask.ext.sqlalchemy import ...
from ..app import app
db = SQLAlchemy(app)
# db/foo.py
from sqlalchemy import ...
from .common import db
class Foo(db.Model):
...
SQLAlchemy Core configuration is exactly the same, except replace class Foo
with your foo_table = Table(...)
declarations. SQLAlchemy 核心配置完全相同,除了用你的
foo_table = Table(...)
声明替换class Foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.