python - NDB 按重复 StructuredProperty 的特定实例的属性排序

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

在一个汽车历史应用程序中,我必须创建不同的图表,其中某些车型可能会出现在一个或多个不同的图表中,如“最快的汽车”、“最好的汽车”等。然后必须在图表中对它们进行排序。我使用 StructuredProperty 创建标签名称/订单位置对。

class CarTag(ndb.Model):
    name = ndb.StringProperty()
    position = ndb.IntegerProperty()

class Car(ndb.Model):
    model_name = ndb.StringProperty()
    trim = ndb.StringProperty()
    year = ndb.IntegerProperty()
    tags = ndb.StructuredProperty(CarTag, repeated=True)

结构化属性上的过滤器工作正常。

cars = Car.query(Car.tags.name=="fastest car")

但是为了获得有序图表,我需要通过名称为“最快的汽车”的同一 StructuredProperty 的位置属性对它们进行排序。正如我在this question中读到的那样order(Car.tags.position) 将仅按列表的第一个元素排序。

cars = Car.query(Car.tags.name==name).order(Car.tags.position)

是否可以按特定 StructuredProperty 的属性排序?

最佳答案

这并不是说您不能按 StructuredProperty 内部的属性进行排序,而是您的 StructuredProperty 重复=True...使其成为列表属性...并且您无法可靠地对列表属性进行排序:

https://cloud.google.com/appengine/docs/python/datastore/queries#properties_with_multiple_values_can_behave_in_surprising_ways

Properties with multiple values can behave in surprising ways

Because of the way they're indexed, entities with multiple values for the same property can sometimes interact with query filters and sort orders in unexpected and surprising ways.

.....

If the query results are sorted in ascending order, the smallest value of the property is used for ordering. If the results are sorted in descending order, the greatest value is used for ordering. Other values do not affect the sort order, nor does the number of values. This has the unusual consequence that an entity with property values 1 and 9 precedes one with values 4, 5, 6, and 7 in both ascending and descending order.

关于python - NDB 按重复 StructuredProperty 的特定实例的属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759920/

相关文章:

java - IntelliJ 2016.3 GAE 部署 Java8/7

python - 在python中将字典写入文件

google-app-engine - golang appengine api测试错误 "appengine: NewContext passed an unknown http.Request"

google-app-engine - 如何将基于 Google 应用引擎的网站迁移到亚马逊平台?

java - org.datanucleus.store.query.QueryCompilerSyntaxException : Identifier expected at character 12 in "u.username== value"

java - 带有字符串通配符的 setFilter()?

java - 数据存储区 MapReduce 是否已弃用

java - 使用 System.loadLibrary 加载 Python C 扩展 DLL

python - 从套接字接收数据时脚本挂起

python - SSH 连接不持久