我可能存储了数千个“报告”模型。我正在尝试获取按 DateTimeProperty 排序的 50 份最新报告。尽管我现有的查询确实按照从新到旧的顺序获取报告,但它不会返回最新 50。而是返回较旧的(已排序)选择。我在这里缺少什么?
class Report(ndb.polymodel.PolyModel):
received_time = ndb.DateTimeProperty(required=True, auto_now_add=True)
report_state = ndb.StringProperty(choices=set(['unresolved', 'resolved']))
...
reports_query = Report.query(
Report.report_state == 'resolved',
ancestor = some_key) \
.order(Report.received_time)
resolved_reports = reports_query.fetch(50)
当我测试时,如果我的数据存储中有 200 个报告并且我获取了 200 个报告,则所有报告都会按正确的顺序排序。
最佳答案
您的查询按 received_time
的升序排序,因此您会得到时间较早的查询(较小的时间 = 较旧的时间)。要获得最旧的,请更改顺序:
.order(-Report.received_time)
关于python - 如何使用 NDB 获取 Google App Engine 中的 N 个最新模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260899/