【问题标题】:Number of entities returned from a GQL Query从 GQL 查询返回的实体数
【发布时间】:2011-05-30 12:43:41
【问题描述】:

我是 Python 新手,目前正在开发我的第一个 Google App Engine 应用程序。在我的程序中,我有一个“收件箱”数据库模型,其中包含 to_user、from_user、title、content 等字符串属性。当用户登录我的应用程序时,我希望能够计算发送给他的消息数量/她,这样我可以将其显示为“新消息(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) 但这给了我一个错误。感谢您的宝贵时间。

【问题讨论】:

标签: python google-app-engine gqlquery


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,这正是我想要的!节日快乐
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 2021-04-23
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多