Peewee Flask 尝试 Return 来自 BooleanField 的数据

Peewee Flask Trying to Return Data from a BooleanField

我正在尝试使用以下代码 return UserTable 模型中用户的 confirmed BooleanField(这样我可以稍后拒绝访问某些路由):

models.py

class UserTable(UserMixin, Model):
    email = CharField(unique=True)
    password = CharField()
    confirmed = BooleanField()

    class Meta:
        database = db

app.py

@app.route('/isconfirmed/<email>')
def isconfirmed(email):
    return models.UserTable.get(models.UserTable.email == email).confirmed

当我尝试这个但是我收到:TypeError: 'bool' object is not callable

我尝试访问 emailpassword

return models.UserTable.get(models.UserTable.email == email).email 等,效果很好。我不明白为什么它不能 return 来自 BooleanField 的 True 或 False???

如果有任何相关性/帮助,我正在使用 Postgres 作为我的数据库。

非常感谢任何帮助!!!

这是正在发生的事情。

models.UserTable.get(models.UserTable.email == email).confirmed 是一个合法的查询,它 return 是一个布尔值 - TrueFalse.

现在,参考view response type handling logic

The return value from a view function is automatically converted into a response object for you. If the return value is a string it’s converted into a response object with the string as response body, an 200 OK error code and a text/html mimetype. The logic that Flask applies to converting return values into response objects is as follows:

  • If a response object of the correct type is returned it’s directly returned from the view.

  • If it’s a string, a response object is created with that data and the default parameters.

  • If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.

  • If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

Flask 从视图中看到布尔值 returned 时,它会尝试将其视为 WSGI 应用程序实例,但失败了。

如果 TrueFalse 是您想要的 return 从视图:

你必须强制它是一个字符串
@app.route('/isconfirmed/<email>')
def isconfirmed(email):
    return str(models.UserTable.get(models.UserTable.email == email).confirmed)

另请参阅此处已解决的相关问题: