我有这个。
@login_manager.user_loader
def load_user(id=None):
return User.query.get(id)
Run Code Online (Sandbox Code Playgroud)
在我介绍 Flask-Principal 之前,它运行良好。
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
# Set the identity user object
identity.user = current_user
# return
if hasattr(current_user, 'id'):
identity.provides.add(UserNeed(current_user.id))
# Assuming the User model has a list of roles, update the
# identity with the roles that the user provides
if hasattr(current_user, 'roles'):
for role in current_user.roles:
identity.provides.add(RoleNeed(role.name))
Run Code Online (Sandbox Code Playgroud)
添加它会导致严重的性能问题。SQLALCHEMY_ECHO 显示每次加载静态文件时都会查询 User 表。
#Warning: Dummy Cache
users = {}
@login_manager.user_loader
def load_user(uid=None):
if uid not in users:
users[uid] = User.query.get(uid)
return users[uid] …Run Code Online (Sandbox Code Playgroud)