Following this example I integrated Flask admin with some already created peewee models. Everything looks great until I try the list view of the admin interface, but first some context:
The important part of the app.py file:
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
The injection of the database object in the package initialization file:
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)
This happens when I try to request the list of clients (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'
I found the issue in my code and it was related to the way i am injecting the Database object into models. I had to use the constructor binding instead of the normal bind as i was binding a lambda function:
binder.bind_to_constructor(Database, lambda: db)