I'm trying to setup flask-admin model views with SQLAlchemy against 'user' and 'role' models. Instead of a function admin view I'm getting:
ValueError: Invalid model property name <class 'app.models.Role'>.desc
Stack trace:
Traceback (most recent call last):
File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/manage.py", line 18, in <module>
app = create_app(os.getenv('APP_CONFIG') or 'default')
File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/app/__init__.py", line 49, in create_app
admin.add_view(RoleAdmin(Role, db.session))
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/view.py", line 288, in __init__
menu_icon_value=menu_icon_value)
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 570, in __init__
self._refresh_cache()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 640, in _refresh_cache
self._refresh_forms_cache()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 580, in _refresh_forms_cache
self._create_form_class = self.get_create_form()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 856, in get_create_form
return self.get_form()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 848, in get_form
return self.scaffold_form()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/view.py", line 607, in scaffold_form
extra_fields=self.form_extra_fields)
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/form.py", line 427, in get_form
for name, p in properties:
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/form.py", line 422, in <genexpr>
properties = ((x, find(x)) for x in only)
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/form.py", line 419, in find
raise ValueError('Invalid model property name %s.%s' % (model, name))
ValueError: Invalid model property name <class 'app.models.Role'>.desc
I am using a flask application factory approach with blueprints. Here's my application factory.
My __init__.py
file where I try and create the views on line 49:
from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security
from flask.ext.admin import Admin
from flask_s3 import FlaskS3
from flask.ext.cdn import CDN
from flask.ext.assets import Environment
from flask.ext.assets import PythonLoader as PythonAssetsLoader
from config import config
# app setup
bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
security = Security()
admin = Admin()
s3 = FlaskS3()
cdn = CDN()
assets_env = Environment()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
app.name = app.config['APP_NAME']
config[config_name].init_app(app)
from .models import user_datastore, User, Role
from .auth.views import RoleAdmin, UserAdmin
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
security.init_app(app, user_datastore)
admin.init_app(app)
s3.init_app(app)
cdn.init_app(app)
assets_env.init_app(app)
# Here's where I'm trying to create the views
admin.add_view(RoleAdmin(Role, db.session))
admin.add_view(UserAdmin(User, db.session))
from . import assets
assets_loader = PythonAssetsLoader(assets)
for name, bundle in assets_loader.load_bundles().iteritems():
assets_env.register(name, bundle)
# attach asset bundles, routes, and custom error pages here
from main import main as main_blueprint
app.register_blueprint(main_blueprint)
import main.assets as main_assets
assets_loader = PythonAssetsLoader(main_assets)
for name, bundle in assets_loader.load_bundles().iteritems():
assets_env.register(name, bundle)
from auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
return app
My ModelView classes:
from flask.ext.admin.contrib.sqla import ModelView
__author__ = 'dbg'
class RoleAdmin(ModelView):
column_display_pk = True
form_columns = ['id', 'desc']
class UserAdmin(ModelView):
column_display_pk = True
form_columns = ['id', 'email', 'active', 'last_login_at', 'login_count', 'roles']
EDIT
Models.py
from app import db
from flask.ext.security import UserMixin, RoleMixin, SQLAlchemyUserDatastore
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer(), primary_key=True)
email = db.Column(db.String(256), unique=True)
password = db.Column(db.String(36))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
last_login_at = db.Column(db.DateTime())
current_login_at = db.Column(db.DateTime())
last_login_ip = db.Column(db.String(45))
current_login_ip = db.Column(db.String(45))
login_count = db.Column(db.Integer())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
UPDATE
Found a very helpful write-up here by this good soul. Still stuck. New error.
Traceback (most recent call last):
File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/manage.py", line 18, in <module>
app = create_app(os.getenv('APP_CONFIG') or 'default')
File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/app/__init__.py", line 83, in create_app
admin.add_view(RoleAdmin(RoleAdmin, db.session))
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/view.py", line 288, in __init__
menu_icon_value=menu_icon_value)
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 570, in __init__
self._refresh_cache()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 632, in _refresh_cache
self._list_columns = self.get_list_columns()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/model/base.py", line 708, in get_list_columns
columns = self.scaffold_list_columns()
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/view.py", line 371, in scaffold_list_columns
for p in self._get_model_iterator():
File "/usr/local/share/anaconda/envs/flaskbase27/lib/python2.7/site-packages/Flask_Admin-1.0.9-py2.7.egg/flask_admin/contrib/sqla/view.py", line 310, in _get_model_iterator
return model._sa_class_manager.mapper.iterate_properties
AttributeError: type object 'RoleAdmin' has no attribute '_sa_class_manager'
description
instead of justdesc
? sorry not quite familiar with it. – Fanchetdesc
column in Role model (you don't have it). Change your RoleAdmin to have form_columns = ['id', 'description'] and it'll work. – Rob