7

「ユーザー」および「ロール」モデルに対してSQLAlchemyを使用してフラスコ管理モデルビューをセットアップしようとしています。関数管理ビューの代わりに、次のようになります。

ValueError: Invalid model property name <class 'app.models.Role'>.desc

スタックトレース:

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

設計図を使用してフラスコ アプリケーション ファクトリ アプローチを使用しています。これが私のアプリケーションファクトリーです。

__init__.py49行目でビューを作成しようとする私のファイル:

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

私のModelViewクラス:

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']

編集

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)

アップデート

この善良な魂による非常に役立つ記事をここで見つけました。まだ立ち往生しています。新しいエラー。

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'
4

2 に答える 2