Flask-Login 引发 TypeError: 'bool' 对象在尝试覆盖 is_active 属性时不可调用 [英] Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_active property
问题描述
我想修改 Flask-Login 中的 is_active
以便用户不总是处于活动状态.
I want to modify is_active
in Flask-Login so that users are not always active.
默认总是返回 True
,但我将其更改为返回 banned
列的值.
The default always returns True
, but I changed it to return the value of the banned
column.
根据文档,is_active
应该是一个属性.但是,内部 Flask-Login 代码引发了:
Based on the docs, is_active
should be a property. However, the internal Flask-Login code raises:
TypeError: 'bool' object is not callable
尝试使用 is_active
时.
如何正确使用 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
和 is_authenticated
都是 Flask 的属性- 登录 0.3.如果要使用它们,请将它们视为属性,不要调用它们.如果你想覆盖它们,记得用 @property
装饰它们.
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
看来您正在阅读最新版本 (0.3) 的文档,但使用的是旧版本的库.0.3 版 包含一项重大更改,将这些属性从方法更改为属性.您应该升级到最新版本的 Flask-Login 并将它们视为属性.
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.
通过使用户的 is_active
属性返回 False
来停用用户.您返回列值的想法很好.
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: 'bool' 对象在尝试覆盖 is_active 属性时不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!