Python 如何检查GQL对象是否为空
我试图检查GQL对象是否为空(在使用Python的Google App Engine中),我应该如何做? 我试过了,但没用:Python 如何检查GQL对象是否为空,python,google-app-engine,gql,Python,Google App Engine,Gql,我试图检查GQL对象是否为空(在使用Python的Google App Engine中),我应该如何做? 我试过了,但没用: comments = db.GqlQuery("SELECT * " "FROM Comment " "where ANCESTOR IS :1 " "order by date D
comments = db.GqlQuery("SELECT * "
"FROM Comment "
"where ANCESTOR IS :1 "
"order by date DESC",
movie_data)
if comments:
ratingsum = 0
i=0
for comment in comments:
ratingsum=ratingsum + comment.rating
i+=1
average = ratingsum/i
else:
average = "no ratings"
Gql查询提供一个查询对象,但不提供结果(实体)。要获取结果,您必须获取结果:使用fetch、count、get或迭代结果集。调用
count
需要执行两次相同的查询。因此,为了避免这种情况,并且由于您必须遍历注释,您还可以通过观察一个副作用来“检测”注释何时为空——如果注释为空,则i
将为零:
comments = db.GqlQuery("SELECT * "
"FROM Comment "
"where ANCESTOR IS :1 "
"order by date DESC",
movie_data)
ratingsum = 0
i = 0
for comment in comments:
ratingsum += comment.rating
i += 1
if i:
average = ratingsum/i
else:
average = "no ratings"