V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lbfeng
V2EX  ›  Flask

Returning the user object in load user function costs too much.

  •  
  •   lbfeng · 2015-12-30 12:25:15 +08:00 · 2036 次点击
    这是一个创建于 3041 天前的主题,其中的信息可能已经有所发展或是发生改变。

    参考了著名的 flasky 中的内容。我在 user table 中扩展出很多内容。 load_user 方法中返回整个 user object 效率变得很低。

    @login_manager.user_loader
    # cost 0.5 ms
    def load_user(user_id):
        return User.query.get(int(user_id))
    

    Miguel 给出的解决方式是返回只有 id 的 user 也可以,并且 implements the required properties (is_authenticated, is_anonymous, etc.)。没搞懂 implements the required properties 怎么搞。我现在返回的 user object 只有 id ,得到 AttributeError: 'result' object has no attribute 'is_authenticated'。怎么搞定 required properties 呢?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2665 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:34 · PVG 23:34 · LAX 08:34 · JFK 11:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.