NDB: Sort query results

2019-07-04 01:40发布

In App Engine NDB, I am querying entities that have a repeated property. I would like to order the result by the length of the array representing that property.

What I wish I could do:

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

2条回答
混吃等死
2楼-- · 2019-07-04 02:05

Depending on how many entities are you sorting you can sort it by code.

查看更多
Anthone
3楼-- · 2019-07-04 02:08

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.

查看更多
登录 后发表回答