getting AttributeError: 'function' object has no attribute 'commit_select' after inject database in a Flask-Admin Peewee web app
getting AttributeError: 'function' object has no attribute 'commit_select' after inject database in a Flask-Admin Peewee web app
在 this example 之后,我将 Flask admin 与一些已经创建的 peewee 模型集成在一起。在我尝试管理界面的列表视图之前,一切看起来都很棒,但首先是一些上下文:
代码
app.py文件的重要部分:
def create_app(config_object=DevConfig):
app = Flask(__name__)
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
register_errorhandlers(app)
return app
def register_extensions(app):
"""Register Flask extensions."""
assets.init_app(app)
bcrypt.init_app(app)
cache.init_app(app)
login_manager.init_app(app)
debug_toolbar.init_app(app)
admin = Admin(app, name=__name__, template_mode='bootstrap3')
__add_admin_views(admin)
return None
def __add_admin_views(admin):
admin.add_view(ClientAdmin(entities.Client))
return None
包初始化文件中数据库对象的注入:
def persistence_config(binder):
db_user = "postgres"
db_name = "MY_DB_NAME"
db_password = "MY_PASS"
db_host = "127.0.0.1"
db_port = 5432
db = PooledPostgresqlExtDatabase(
database=db_name,
user=db_user,
host=db_host,
password=db_password,
max_connections=8,
stale_timeout=300,
register_hstore=False)
binder.bind(Database, lambda: db)
错误
当我尝试请求客户列表 (http://127.0.0.1:5000/admin/client/) 时会发生这种情况:
Traceback (most recent call last):
File "MY_ENV_PATH/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "MY_ENV_PATH/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "MY_ENV_PATH/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "MY_ENV_PATH/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/base.py", line 68, in inner
return self._run_view(f, *args, **kwargs)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/base.py", line 367, in _run_view
return fn(self, *args, **kwargs)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/model/base.py", line 1738, in index_view
view_args.search, view_args.filters)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/contrib/peewee/view.py", line 353, in get_list
query = self.get_query()
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/contrib/peewee/view.py", line 328, in get_query
return self.model.select()
File "MY_ENV_PATH/lib/python2.7/site-packages/peewee.py", line 4458, in select
query = SelectQuery(cls, *selection)
File "MY_ENV_PATH/lib/python2.7/site-packages/peewee.py", line 2696, in __init__
self.require_commit = self.database.commit_select
AttributeError: 'function' object has no attribute 'commit_select'
我在我的代码中发现了这个问题,它与我将数据库对象注入模型的方式有关。我不得不使用构造函数绑定而不是普通绑定,因为我绑定了一个 lambda 函数:
binder.bind_to_constructor(Database, lambda: db)
在 this example 之后,我将 Flask admin 与一些已经创建的 peewee 模型集成在一起。在我尝试管理界面的列表视图之前,一切看起来都很棒,但首先是一些上下文:
代码
app.py文件的重要部分:
def create_app(config_object=DevConfig):
app = Flask(__name__)
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
register_errorhandlers(app)
return app
def register_extensions(app):
"""Register Flask extensions."""
assets.init_app(app)
bcrypt.init_app(app)
cache.init_app(app)
login_manager.init_app(app)
debug_toolbar.init_app(app)
admin = Admin(app, name=__name__, template_mode='bootstrap3')
__add_admin_views(admin)
return None
def __add_admin_views(admin):
admin.add_view(ClientAdmin(entities.Client))
return None
包初始化文件中数据库对象的注入:
def persistence_config(binder):
db_user = "postgres"
db_name = "MY_DB_NAME"
db_password = "MY_PASS"
db_host = "127.0.0.1"
db_port = 5432
db = PooledPostgresqlExtDatabase(
database=db_name,
user=db_user,
host=db_host,
password=db_password,
max_connections=8,
stale_timeout=300,
register_hstore=False)
binder.bind(Database, lambda: db)
错误
当我尝试请求客户列表 (http://127.0.0.1:5000/admin/client/) 时会发生这种情况:
Traceback (most recent call last):
File "MY_ENV_PATH/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "MY_ENV_PATH/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "MY_ENV_PATH/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "MY_ENV_PATH/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "MY_ENV_PATH/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/base.py", line 68, in inner
return self._run_view(f, *args, **kwargs)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/base.py", line 367, in _run_view
return fn(self, *args, **kwargs)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/model/base.py", line 1738, in index_view
view_args.search, view_args.filters)
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/contrib/peewee/view.py", line 353, in get_list
query = self.get_query()
File "MY_ENV_PATH/lib/python2.7/site-packages/flask_admin/contrib/peewee/view.py", line 328, in get_query
return self.model.select()
File "MY_ENV_PATH/lib/python2.7/site-packages/peewee.py", line 4458, in select
query = SelectQuery(cls, *selection)
File "MY_ENV_PATH/lib/python2.7/site-packages/peewee.py", line 2696, in __init__
self.require_commit = self.database.commit_select
AttributeError: 'function' object has no attribute 'commit_select'
我在我的代码中发现了这个问题,它与我将数据库对象注入模型的方式有关。我不得不使用构造函数绑定而不是普通绑定,因为我绑定了一个 lambda 函数:
binder.bind_to_constructor(Database, lambda: db)