简体   繁体   English

通过重复的StructuredProperty的特定实例的属性对NDB进行排序

[英]NDB ordering by property of specific instance of repeated StructuredProperty

In a an app for cars history I have to create different charts where some models may be present in one or more different charts as "fastest car", "best car", etc. Then they have to be ordered in the chart. 在用于汽车历史记录的应用程序中,我必须创建不同的图表,其中某些模型可能以“最快的汽车”,“最佳的汽车”等形式出现在一个或多个不同的图表中。然后必须在图表中对其进行排序。 I have used StructuredProperty to create tag name/order position pairs. 我已经使用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)

The filter on the structured property works fine. 结构化属性上的过滤器工作正常。

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

But to get the ordered chart I need to order them by the position property of the same StructuredProperty which name is "fastest car". 但是要获得排序的图表,我需要通过相同的StructuredProperty的position属性(即“最快的汽车”)对它们进行排序。 As I read in this question order(Car.tags.position) will order only by the first element of the list. 如我在此问题中所读,order(Car.tags.position)将仅按列表的第一个元素进行排序。

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

Is it possible to order by property of specific StructuredProperty? 是否可以通过特定StructuredProperty的属性进行订购?

It's not that you can't order by a property inside of a StructuredProperty, it's that your StructuredProperty is repeated=True... making it a list-property.. and you can't reliably sort on a list-property: 这并不是说您不能按StructuredProperty内部的属性排序,而是您的StructuredProperty重复= True ...使其成为列表属性..并且您不能可靠地对列表属性进行排序:

https://cloud.google.com/appengine/docs/python/datastore/queries#properties_with_multiple_values_can_behave_in_surprising_ways 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. 这具有不同寻常的结果,即属性值1和9的实体在值4、5、6和7之前都以升序和降序排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM