NDB:对查询结果进行排序

时间:2015-09-14 14:40:43

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

在App Engine NDB中,我正在查询具有重复属性的实体。我想按表示该属性的数组的长度来排序结果。

我希望我能做什么:

Entity.query(...).order(len(Entity.repeatedProp))

2 个答案:

答案 0 :(得分:3)

You'll need to add an ndb.IntegerProperty() to your entity where you will store the length of the repeated property. Every time you change your repeated property, you'll need to update the stored length. Then you sort based on that stored length.

You could probably use a computed property, but I've never used one of those so I'm not sure.

答案 1 :(得分:0)

根据您排序的实体数量,您可以按代码对其进行排序。