Number of objects returned from a GQL query

I am new to Python and am currently working on my first Google App Engine application. In my program, I have an Inbox database model that contains row restrictions such as to_user, from_user, title, content, etc. When a user logs into my application, I want to be able to count the number of messages that were sent to him / her, so I can display it as "New Messages (x)". I feel that I am currently using work because I cannot 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. Thank you for your time.

+3
source share
1 answer

In your particular case, where the number of new messages is likely to be small, I would not create an upfront counter as suggested here .
I would go with a simpler solution using count () :

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

Source: https://habr.com/ru/post/1782033/


All Articles