Flask-Login引发TypeError:当试图覆盖is_active属性时,“bool”对象不可调用 [英] Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_active property

查看:314
本文介绍了Flask-Login引发TypeError:当试图覆盖is_active属性时,“bool”对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flask-Login中修改 is_active ,这样用户并不总是处于活动状态。

默认总是返回 True ,但我改变它返回禁止栏。

基于文档, is_active 应该是一个属性。然而,内部的Flask-Login代码引发了:

$ p $ TypeError:'bool'对象不可调用

尝试使用 is_active 时。



如何正确使用 is_active 来停用某些使用者?
$ b $ (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
pre>




  login_user(user,form.remember_me.data)

如果不是强制,而不是user.is_active():
TypeError:'bool'对象不可调用


解决方案

is_active is_anonymous is_authenticated 是Flask-Login 0.3的所有属性。如果你想使用它们,把它们作为属性,不要叫它们。如果你想覆盖它们,记得用 @property 来装饰它们。

 #从$ b $改变current_user.is_authenticated()
#改变
current_user.is_authenticated

看来您正在阅读最新版本(0.3)的文档,但使用的是旧版本的库。版本0.3 包含重大更改,将方法的这些属性更改为属性。您应该升级到最新版本的Flask-Login,并将它们视为属性。



通过使用户的 is_active 属性返回 False 。你的想法返回一列的价值是好的。


I want to modify is_active in Flask-Login so that users are not always active.

The default always returns True, but I changed it to return the value of the banned column.

Based on the docs, is_active should be a property. However, the internal Flask-Login code raises:

TypeError: 'bool' object is not callable 

When trying to use is_active.

How do I correctly use is_active to deactivate some users?

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


login_user(user, form.remember_me.data)

if not force and not user.is_active():
TypeError: 'bool' object is not callable

解决方案

is_active, is_anonymous, and is_authenticated are all properties as of Flask-Login 0.3. If you want to use them, treat them as attributes, don't call them. If you want to override them, remember to decorate them with @property.

# change from
current_user.is_authenticated()
# to
current_user.is_authenticated

It appears you are reading the docs for the most recent version (0.3), but using an older version of the library. Version 0.3 contains a breaking change which changed these attributes from methods to properties. You should upgrade to the latest version of Flask-Login and treat them as properties.

You deactivate the user by causing its is_active property to return False. Your idea to return the value of a column is fine.

这篇关于Flask-Login引发TypeError:当试图覆盖is_active属性时,“bool”对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆