python - Google 应用引擎如何从数据存储中计算 SUM?

标签 python google-app-engine gql

我想知道,如何获得从数据存储 (python) 中获得的评级实体的总和?

我应该:

ratingsum = 0
for rating in ratings:
    ratingsum + rating

print ratingsum

?

最佳答案

是的,差不多就是这样。检索您想要求和的所有实体,并在您的应用程序中对它们求和。 GQL 中没有SUM

如果您想要完成的是找到某个实体的平均评分,那么有更好的方法。

class RateableThing(db.Model):
    num_ratings = db.IntegerProperty()
    avg_rating = db.FloatProperty()

查找事物的平均评分是一个简单的查询,添加一个新的评分只是:

thing.avg_ratings = ((thing.avg_ratings * thing.num_ratings) + new_rating) / thing.num_ratings + 1
thing.num_ratings += 1
thing.put()

App Engine 数据存储区的流行习惯是尽可能多地写入,尽可能少地读取,因为读取会更频繁地发生。

关于python - Google 应用引擎如何从数据存储中计算 SUM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686361/

相关文章:

python Tkinter 列表框回调 : passing a class-instance-specific callback

python:从带括号的边创建图形节点

python - 如何巧妙地重写曾经使用 XSLT 的 python 脚本?

google-cloud-datastore - 数据存储 : `SELECT * FROM Entity WHERE property IS NULL` returns no result despite entities without property

python - AppEngine 中的 GQL 查询

python - 如何在 Sikuli 中使用 Python OpenCV 函数

Python 3.5 setup.py egg_info 失败,错误代码为 1 [Linux]

google-app-engine - 谷歌应用引擎 : efficient large deletes (about 90000/day)

python - Google Appengine 标准 Python 2.7 : Can't run Google Endpoints on localhost dev_appserver. py 不再

ruby-on-rails - 如何使 Google App Engine 上的 Rails 应用程序重定向到 HTTPS