Peewee 和 Flask : 'Database' object 没有属性 'commit_select'

Peewee and Flask : 'Database' object has no attribute 'commit_select'

我正在尝试将 Peewee 与 Flask 一起使用,但我不明白为什么我的数据库连接不起作用。

config.py

class Configuration(object):
DATABASE = {
    'name': 'test',
    'engine': 'peewee.MySQLDatabase',
    'user': 'root',
    'passwd': 'root'
}
DEBUG = True
SECRET_KEY = 'shhhh'

app/init.py

from flask import Flask
from flask_peewee.db import Database
app = Flask(__name__)
app.config.from_object('config.Configuration') 
db = Database(app)
import views,models

app/models.py

from peewee import *
from . import db
database = db

class UnknownField(object):
    def __init__(self, *_, **__): pass
class BaseModel(Model):
    class Meta:
        database = database

class Tbcategory(BaseModel):
    insert_dt = DateTimeField()
    name = CharField()

    class Meta:
        db_table = 'tbcategory'

我用 pwiz 生成了 models.py。

如果我尝试在交互式控制台上使用它,我会在标题上看到错误。如果我将 models.py 上的行从 database=db 更改为 pwiz 创建的原始行:

db = MySQLDatabase('test', **{'host': '127.0.0.1', 'password': 'root', 'user': 'root'})

一切正常。我一生都找不到互联网上的例子。配置要么全部在应用程序中,要么在 config.py 文件中,但使用 sqlite 或其他一些略有不同的用法。 我应该停止使用 flask_peewee 中的 Database() 并直接使用 MySQLDatabase 吗?我如何在配置中使用外部文件? 请注意,我在一种方法上使用 127.0.0.1,而在另一种方法上没有指定主机。我确实从 peewee-flask 的网站上复制了内容。

您正在使用 Database 包装器对象。要获取实际的 Peewee 数据库对象,请使用:

app.config.from_object('config.Configuration') 
db = Database(app)
database = db.database  # database is the actual peewee database obj.

在您的模型代码中:

from peewee import *
from . import database  # Import the actual peewee database