Flask-Login引发TypeError:尝试覆盖is_active属性时,'bool'对象不可调用

anv*_*nvd 8 python flask flask-login

我想is_active在Flask-Login中进行修改,以便用户不会始终处于活动状态.

默认值始终返回True,但我更改了它以返回banned列的值.

根据文档,is_active应该是一个属性.但是,内部Flask-Login代码引发:

TypeError: 'bool' object is not callable 
Run Code Online (Sandbox Code Playgroud)

试图使用时is_active.

如何正确使用is_active停用某些用户?

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    banned = db.Column(db.Boolean, default=False)

    @property
    def is_active(self):
        return self.banned
Run Code Online (Sandbox Code Playgroud)
login_user(user, form.remember_me.data)

if not force and not user.is_active():
TypeError: 'bool' object is not callable
Run Code Online (Sandbox Code Playgroud)

dav*_*ism 24

is_active,is_anonymous和,is_authenticated都是Flask-Login 0.3的所有属性.如果要使用它们,请将它们视为属性,不要调用它们.如果你想覆盖它们,记得用它们装饰它们@property.

# change from
current_user.is_authenticated()
# to
current_user.is_authenticated
Run Code Online (Sandbox Code Playgroud)

您似乎正在阅读最新版本(0.3)的文档,但使用的是旧版本的库.版本0.3 包含一个重大更改,它将这些属性从方法更改为属性.您应升级到最新版本的Flask-Login并将其视为属性.

您通过使其is_active属性返回来停用该用户False.您想要返回列的值是没问题的.