AttributeError:'SelectQuery'对象没有属性'is_active'

时间:2014-01-15 14:22:15

标签: python flask attributeerror peewee flask-peewee

我正在尝试按照Flask Mega Tutorial与Flask一起学习Peewee ORM。在part 5 of the tutorial中,我使用OpenID创建登录。在克服了许多障碍之后,我现在在下面的行中粘贴的函数中得到一个AttributeError:login_user(user, remember = remember_me)

@oid.after_login
def after_login(resp):
    if resp.email is None or resp.email == "":
        flash('Invalid login. Please try again.')
        return redirect(url_for('login'))
    user = User.select().where(User.email == resp.email)    
    if user.wrapped_count() == 0:
        nickname = resp.nickname
        if nickname is None or nickname == "":
            nickname = resp.email.split('@')[0]
        user = User(nickname = nickname, email = resp.email, role = models.ROLE_USER)
        user.save()
    remember_me = False
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)
    login_user(user, remember = remember_me)
    return redirect(request.args.get('next') or url_for('index'))
在我的用户模型中可以找到

is_active,如下所示:

class User(db.Model):
    nickname = TextField()
    email = TextField()
    role = IntegerField(default = ROLE_USER)

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    def __repr__(self):
        return '<User %r>' % (self.nickname)

我不知道我在这里做错了什么。有人能帮助我解决我在这里做错的事吗?

欢迎所有提示!

1 个答案:

答案 0 :(得分:9)

如错误所示,user = User.select().where(User.email == resp.email)会返回SelectQuery,而不是User的实例。您需要包含一个额外的方法调用来实际获取记录,例如.first()first将返回UserNone的实例。

这样您就可以稍微调整一下代码:

user = User.select().where(User.email == resp.email).first()
if not user:  # or if user is None:
    nickname = resp.nickname
    ...