Appengine滤波器不等式与排序失败

2024-05-16 23:07:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想我忽略了一些简单的事情,我无法想象这是不可能做到的。

我想通过datetime属性进行筛选,然后通过ranking integer属性对结果进行排序。当我尝试这样做时:

query.filter("submitted >=" thisweek).order("ranking")

我得到以下信息:

BadArgumentError: First ordering property must be the same as inequality filter property, if specified for this query; received ranking, expected submitted

嗯?我错过了什么?

谢谢。


Tags: 信息datetime属性排序orderpropertyintegerfilter
3条回答

数据存储无法按不等式中使用的属性以外的任何属性对包含不等式的查询进行排序。

这通常可以通过添加一个可以用相等值过滤的属性来解决;在这种情况下,可能有一个布尔属性来跟踪某个实体是否来自当前周,并在每个周末为所有实体更新它。

我使用了另一个技巧,它的成功仅仅是因为我需要我的数据格式(一个dict列表)。在本例中,我运行基于日期时间的查询,从返回的ents创建dict,然后按数字“counter”属性排序。颠倒排序给了我一个降序。请记住,我只要求10个结果,在一个相当小的数据存储。

q = food.Food.all()
q.filter("last_modified <=", now)
q.filter("last_modified >=", hour_ago)

ents = q.fetch(10)

if ents:
  results = [{
    "name": ent.name,
    "counter": ent.counter
    } for ent in ents]

  # reverse list for 'descending' order
  results.sort(reverse=True)

示例结果:

[{'counter': 111L, 'name': u'wasabi'}, {'counter': 51L, 'name': u'honeydew'}, {'counter': 43L, 'name': u'mars bar'}, {'counter': 37L, 'name': u'scallop'}, {'counter': 33L, 'name': u'turnip'}, {'counter': 29L, 'name': u'cornbread'}, {'counter': 16L, 'name': u'mackerel'}, {'counter': 10L, 'name': u'instant coffee'}, {'counter': 3L, 'name': u'brussel sprouts'}, {'counter': 2L, 'name': u'anchovies'}]

数据存储对查询有一些限制。一种是不允许将一个属性上的不等式过滤器与另一个属性上的顺序结合起来。您可以在此处找到更多限制:

https://cloud.google.com/appengine/docs/python/ndb/queries

相关问题 更多 >