meh*_*kar 6 google-app-engine djangoappengine app-engine-ndb
这似乎应该是一个简单的问题.但是文档似乎没有回答它.使用他们的例子,我想这样做:
Account.query(Account.title == "best")
除了我想匹配部分字符串.所以在这种情况下:
acct = Account(title="the best account in the world")
带参数"best"的ndb查询将匹配acct.
我看到目前唯一的选择是遍历Account.query()并相互匹配title与re.search在Python模块.这似乎不是一个好的解决方案.
更新:我也在看gql.这样做:
acct = ndb.gql('SELECT * from Account WHERE title LIKE '%best%')
返回一个 Parse Error: Invalid WHERE Condition at symbol LIKE
注意:这并不能完全回答这个问题,但是有人在starts with寻找这个答案可能会有用.
NDB的String字段的索引方式是您可以执行大于(>=)和小于(<)的搜索.假设以下Person型号:
class Person(ndb.Model):
    name         = ndb.StringProperty()
    name_lower   = ndb.ComputedProperty(lambda self: self.name.lower())
您可以执行以下操作:
def search_by_text(text):
  text = text.lower()
  limit = text[:-1] + chr(ord(text[-1]) + 1)
  return Person.query(Person.name_lower >= text, Person.name_lower < limit).fetch(50)
p = search_by_text('kri')
limit此示例中的变量将包含字符串'krj'并成为搜索值的限制.以上内容将为您提供名称大于kri但小于krj并限制前50个发现的所有人.由于限制,名称喜欢kross和lark将被过滤掉.
注意:您必须ndb.ComputedProperty包含要搜索的字段的小写版本.别忘了添加!
| 归档时间: | 
 | 
| 查看次数: | 5573 次 | 
| 最近记录: |