如何将StringProperty迭代为字符串对象?

问题描述 投票:0回答:2

我的代码如下:

class sample(ndb.Model):
    text=ndb.StringProperty()

class sample2(webapp2.RequestHandler):
   def get(self, item):
       q = sample.query()
       q1 = query.filter(item in sample.text)

我想在样本中的文本中搜索特定的单词(项目)。我该怎么办?我尝试了此链接,但它并没有真正回答我的问题-How do I get the value of a StringProperty in Python for Google App Engine?

python string python-2.7 google-app-engine webapp2
2个回答
0
投票

[遗憾的是,您无法通过数据存储区(或其他任何数据库)进行类似的查询。数据存储区查询基于索引,并且索引无法存储复杂的信息,例如字符串是否具有特定的子字符串。

App Engine还具有Search API,可用于进行更复杂的文本搜索。


-1
投票

首先,自2020年1月1日起Python 2 is deprecated,并且Google Cloud Platform强烈建议将migration更新为运行时。另外,不再建议使用ndb库,并且在开始使用Python 3运行时之前,需要更新依赖它的代码。所有这些原因使我建议您在花费大量时间之前直接进行切换。

话虽如此,您可以在ndb库文档中找到有关queriesproperties的答案。 Github code samples中也有几个示例。

首先,您需要使用query()方法检索实体,然后通过python对象属性表示法访问其数据。或多或少它将看起来像下面:

q = sample.query()            # Retrieve sample entity.
substring = 'word' in q.text  # Returns True if word is a substring of text
© www.soinside.com 2019 - 2024. All rights reserved.