gpt4 book ai didi

python - 从 GQL 查询返回的实体数

转载 作者:行者123 更新时间:2023-11-28 17:53:48 24 4
gpt4 key购买 nike

我是 Python 的新手,目前正在开发我的第一个 Google App Engine 应用程序。在我的程序中,我有一个“收件箱”数据库模型,其中包含字符串属性,例如 to_user、from_user、title、content 等。当用户登录到我的应用程序时,我希望能够计算发送给他的消息数量/她,这样我就可以将其显示为“New Messages(x)”。我觉得我目前正在使用变通方法,因为我找不到更好的方法。

user = users.get_current_user()
inbox = Inbox.gql('WHERE to_user = :to_user', to_user=user.nickname())
count = 0
for note in inbox:
count = count+1

我尝试使用 len(inbox) 但它给了我一个错误。感谢您的宝贵时间。

最佳答案

在您的特定情况下,新消息的数量可能很少,我不会费心预先创建一个计数器 as suggested here .
我会使用一个更简单的解决方案 count()功能:

user = users.get_current_user()
inbox = Inbox.gql('WHERE to_user = :to_user', to_user=user.nickname())
count = inbox.count()

关于python - 从 GQL 查询返回的实体数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4528434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com