按特定属性的重复排序

2024-04-28 14:41:07 发布

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

在一个汽车历史应用程序中,我必须创建不同的图表,其中一些车型可能会出现在一个或多个不同的图表中,如“最快的汽车”、“最好的汽车”等,然后它们必须在图表中排序。我使用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)

structured属性上的过滤器工作正常。在

^{pr2}$

但是为了得到有序的图表,我需要根据同一个结构属性的position属性对它们进行排序,这个属性的名称是“最快的汽车”。{按顺序读}(汽车标签位置)将仅按列表的第一个元素排序。在

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

是否可以按特定结构属性的属性排序?在


Tags: namemodel属性排序顺序图表tagsposition
1条回答
网友
1楼 · 发布于 2024-04-28 14:41:07

不是你不能按StructuredProperty内部的一个属性排序,而是你的StructuredProperty是repeated=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.

相关问题 更多 >