Question

I use the following code to count number of entities:

def get_emails_count(email_type=None, yesterday=None, status=None):
    emails = EmailQueue.query()
    if email_type:
        emails = emails.filter(EmailQueue.email_type == email_type)
    if yesterday:
        ...
    if status:
        emails = emails.filter(EmailQueue.status == status)
    emails = emails.count(keys_only=True)
    return emails

get_emails_count(email_type=4, status=0)

And the following GQL query (used at GAE console):

SELECT __key__ FROM EmailQueue WHERE email_type=4 and status=0

The first code returns 245, the second one returns 5 entities (which looks more correct). What is wrong with the first code?

Was it helpful?

Solution

Be careful about using implicit None comparison:

if status:
  emails = emails.filter(EmailQueue.status == status)

Note that if 0: will evaluate to False. Instead, you should write:

if status is not None:
  emails = emails.filter(EmailQueue.status == status)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top