我使用以下代码来计算实体数量:
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)
以下GQL查询(在GAE控制台中使用):
SELECT __key__ FROM EmailQueue WHERE email_type=4 and status=0
第一个代码返回245,第二个代码返回5个实体(看起来更正确)。第一个代码出了什么问题?
答案 0 :(得分:1)
小心使用隐式无比较:
if status:
emails = emails.filter(EmailQueue.status == status)
请注意,if 0:
将评估为False
。相反,你应该写:
if status is not None:
emails = emails.filter(EmailQueue.status == status)