Flask + Peewee:在哪里创建表?
Flask + Peewee: where to create tables?
而不是 flask-peewee 我使用普通的 peewee 包。
这是我初始化数据库的方式:
import os
# just extending the BaseFlask with yaml config reader
from . extensions.flask_app import Flask
# peewee's wrapper around the database
from playhouse.flask_utils import FlaskDB
db_wrapper = FlaskDB()
# define the application factory
def create_app(env):
app = Flask(__name__)
# load config depending on the environment
app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
# init extensions
db_wrapper.init_app(app)
# ...
我知道我应该调用它来创建 tables:
from . models import User
db_wrapper.database.connect()
db_wrapper.database.create_tables([User])
但是我应该把table创建代码放在哪里,这样数据库就已经初始化了?
编辑
查看 docs 我发现我可以这样使用 User.create_table(fail_silently=True)
:
# in app/__init__.py
# define the application factory
def create_app(env):
app = Flask(__name__)
# load config depending on the environment
app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
# init extensions
db_wrapper.init_app(app)
create_tables();
# rest of the initialization
def create_tables():
from . models import User
User.create_table(fail_silently=True)
在这里做可以吗?或者有更好的 way/tool 吗?
编辑
想通了。请看下面我的回答。
更新
我不知道 Flask 中的内置 CLI 支持。我完全不知道您是否应该考虑这种方法,因为您可以开箱即用(参见 documntation)。
我可以使用 flask-script 包。我以前做过,只是忽略了。
激活您的虚拟环境和运行:
pip install flask-script
然后在你的根目录下创建manage.py文件,添加这些行:
import os
from app import create_app, db_wrapper
from app.models import *
from flask_script import Manager, Shell
# create the application instance
app = create_app(os.getenv('FLASK_ENV', 'development'))
# instantiate script manager
manager = Manager(app)
def make_shell_context():
return dict(app=app, db_wrapper=db_wrapper, User=User)
@manager.command
def run_shell():
Shell(make_context = make_shell_context).run(no_ipython=True, no_bpython=True)
# here's my simple command
@manager.command
def create_tables():
User.create_table(fail_silently=True)
@manager.command
def run_tests():
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
# run it
if __name__ == '__main__':
manager.run()
而不是 flask-peewee 我使用普通的 peewee 包。
这是我初始化数据库的方式:
import os
# just extending the BaseFlask with yaml config reader
from . extensions.flask_app import Flask
# peewee's wrapper around the database
from playhouse.flask_utils import FlaskDB
db_wrapper = FlaskDB()
# define the application factory
def create_app(env):
app = Flask(__name__)
# load config depending on the environment
app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
# init extensions
db_wrapper.init_app(app)
# ...
我知道我应该调用它来创建 tables:
from . models import User
db_wrapper.database.connect()
db_wrapper.database.create_tables([User])
但是我应该把table创建代码放在哪里,这样数据库就已经初始化了?
编辑
查看 docs 我发现我可以这样使用 User.create_table(fail_silently=True)
:
# in app/__init__.py
# define the application factory
def create_app(env):
app = Flask(__name__)
# load config depending on the environment
app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
# init extensions
db_wrapper.init_app(app)
create_tables();
# rest of the initialization
def create_tables():
from . models import User
User.create_table(fail_silently=True)
在这里做可以吗?或者有更好的 way/tool 吗?
编辑
想通了。请看下面我的回答。
更新
我不知道 Flask 中的内置 CLI 支持。我完全不知道您是否应该考虑这种方法,因为您可以开箱即用(参见 documntation)。
我可以使用 flask-script 包。我以前做过,只是忽略了。
激活您的虚拟环境和运行:
pip install flask-script
然后在你的根目录下创建manage.py文件,添加这些行:
import os
from app import create_app, db_wrapper
from app.models import *
from flask_script import Manager, Shell
# create the application instance
app = create_app(os.getenv('FLASK_ENV', 'development'))
# instantiate script manager
manager = Manager(app)
def make_shell_context():
return dict(app=app, db_wrapper=db_wrapper, User=User)
@manager.command
def run_shell():
Shell(make_context = make_shell_context).run(no_ipython=True, no_bpython=True)
# here's my simple command
@manager.command
def create_tables():
User.create_table(fail_silently=True)
@manager.command
def run_tests():
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
# run it
if __name__ == '__main__':
manager.run()