开发者

Number of entities returned from a GQL Query

I'm new to Python and currently working on my first Google App Engine application. In my pro开发者_运维技巧gram 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.


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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜