简体   繁体   English

在带有 SQLAlchemy Core 和 Flask 的项目中,我在哪里以及如何使用 Table() 调用?

[英]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:我特别挣扎的是以下几点:

  1. Should I put metadata = MetaData() into tables/__init__.py and import it in the tables/__init__.py files?我应该将metadata = MetaData()放入tables/__init__.py并将其导入tables/__init__.py文件吗?
  2. Am I supposed to simply import the instances from the tables/tablesX.py files if I want to access my Tables?如果我想访问我的表,我是否应该简单地从tables/tablesX.py文件中导入实例?
  3. The perfect solution for me would be to put all the tables into the metadata object.对我来说完美的解决方案是将所有表放入元数据对象中。 If I then need access to a table in eg a view I could simply pull the table out of the metadata with 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.但我不知道如何传递元数据对象以保存所有表。
  4. Or am I completly on the wrong path here and am I supposed to create the table description (ie call table = Table(name, tmp_metadata,...) directly in my views.py files?还是我在这里完全走错了路,我是否应该直接在我的 views.py 文件中创建表描述(即调用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.

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