从 GQL 查询返回的实体数量
我是 Python 新手,目前正在开发我的第一个 Google App Engine 应用程序。在我的程序中,我有一个“收件箱”数据库模型,其中包含字符串属性,例如 to_user、from_user、标题、内容等。当用户登录到我的应用程序时,我希望能够计算发送给他的消息数/她,这样我就可以将其显示为“新消息(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) 但这给了我一个错误。感谢您抽出时间。
I'm new to Python and currently working on my first Google App Engine application. In my program I have an 'Inbox' database model which contains string proporties like to_user, from_user, title, content, etc. When a user logs in to my app I want to be able to count the number of messages that were sent to him/her, this way I can display it as "New Messages(x)". I feel like I currently am using a work around because I can't find a better way.
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
I tried using len(inbox) but that gave me an error. Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的具体情况下,新消息的数量可能很小,我不会费心预先创建一个计数器 按照此处的建议。
我会使用 count( ) 功能:
In your specific case, where the number of New Messages will probably be small, I would not bother to create a counter upfront as suggested here.
I would go with a simpler solution using count() function: