mic*_*ael 1 python flask flask-sqlalchemy flask-security
我正在尝试使用 flask_security 添加一些角色/用户,但是当我尝试使用SQLAlchemySessionUserDatastore. 所以我首先user_datastore像指南一样创建
db = SQLAlchemy(app)
user_datastore = SQLAlchemySessionUserDatastore(db, User, Role)
security = Security(app, user_datastore)
user_datastore.create_user(email='test', password='passowrd')
Run Code Online (Sandbox Code Playgroud)
这是我偏离指南并尝试只创建一个测试用户的地方。看起来db = SQLAlchemy(app)是导致我的问题。
self.db.session.add(model)
AttributeError: 'SQLAlchemy' object has no attribute 'add'
Run Code Online (Sandbox Code Playgroud)
小智 5
user_datastore = SQLAlchemySessionUserDatastore(db, User, Role)
上面的行可能是这里的罪魁祸首。
我的决议如下: user_datastore = SQLAlchemySessionUserDatastore( db.session , User, Role)