3

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.

user553565
  • 77
  • 2
  • check this question: http://stackoverflow.com/questions/421751/whats-the-best-way-to-count-results-in-gql – DanJ Dec 24 '10 at 22:01

1 Answers1

2

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()
Community
  • 1
  • 1
systempuntoout
  • 71,966
  • 47
  • 171
  • 241