是否可以根据 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/