python - 在数据存储中获取最近 7 天的实体

标签 python google-app-engine google-cloud-datastore

我需要获取过去 7 天的实体,如果获取的实体少于 50 个,则将范围扩展到 10 天,该模型类似于:

class MyModel(db.Model):
    title = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    plays = db.IntegerProperty()

我还需要按播放次数降序排列。

感谢任何帮助 谢谢 J

最佳答案

我现在无法测试代码,但我会做类似的事情:

from datetime import datetime, timedelta

query = MyModel.gql("WHERE date > :1 ORDER BY plays DESC", datetime.now() - timedelta(days=7))
count = query.count()
if count < 50:
   query = MyModel.gql("WHERE date > :1", datetime.now() - timedelta(days=10))
value = query.fetch()

关于python - 在数据存储中获取最近 7 天的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244814/

相关文章:

python - 更改 argparse 使用消息参数顺序

python - 将 python 可视化面板保存为 html

python - 错误 : expected declaration specifiers or '...' before string constant

google-app-engine - 如何使用 Google App Engine NDB 获得字段的最大值

google-app-engine - 无法创建 Google Cloud Console 项目

google-app-engine - 使用高复制数据存储 + NDB 进行写入/读取

Python TextWrapper,给定 n 行

java - 如何使用 Apache POI 通过 Google App Engine 读取电子表格?

indexing - 禁用 Google Cloud Datastore 中的内置索引

java - Google App Engine 中的 JDO : How should polymorphic relationships be implemented?