python - Google App Engine ndb 按 StringProperty 长度排序

标签 python google-app-engine sql-order-by google-cloud-datastore app-engine-ndb

是否可以根据 StringProperty 的长度对查询进行排序?我试过这个:

User.query(User.facebook_id = fb_id).order(-len(str(User.name))).fetch(10) 

但它向我显示错误:TypeError:order()需要一个属性或查询订单;

最佳答案

您也许可以使用Computed Property :

name_len = ndb.ComputedProperty(lambda self: len(self.name))

然后用它进行查询:

User.query(User.facebook_id = fb_id).order(-User.name_len).fetch(10)

如果这不起作用,那么您需要添加一个 IntegerProperty 并将其设置为字符串的长度 (name_len = ndb.IntegerProperty())

关于python - Google App Engine ndb 按 StringProperty 长度排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021975/

相关文章:

python - 多重处理需要更长的时间?

python - Python 2.7 中的线程

google-app-engine - 删除和更改 google appengine 端点

sql - Postgresql首先显示特定值,按不同标准排序

python - ': [Errno 2] No such file or directoryile ' in Linux

当控制台具有焦点时,Python 使用 Tkinter 捕获事件

google-app-engine - 使用 ReferenceProperty 的 "IN' 查询构建 GQL

java - 应用引擎 : Having a web module and a endpoint module

sql - SQL Server 中的 Order By 将正值放在负值之前

sql - 为什么在 'month' 中使用 'where' 时 SQL Server 返回不同的顺序?