我有一个模型,表示为
class Suggestion(db.Model):
text = db.TextProperty()
votes = db.IntegerProperty()
time_added = db.DateTimeProperty(auto_now_add=True)
time_modified = db.DateTimeProperty(auto_now=True)
我添加了一个建议
suggestion = Suggestion(text='Adding Suggestion', votes=1)
suggestion.put()
我看到该值已插入,现在我想通过查询文本属性来获取此建议。我做了以下事情
from models import Suggestion
suggestion = Suggestion.all().filter('text = ', 'Adding Suggestion').fetch(1)[0]
print suggestion
结果为空。我怎样才能使这个查询工作?
谢谢
最佳答案
这是由于文本属性不能在过滤器中使用。 http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#TextProperty
Unlike StringProperty, a TextProperty value can be more than 500 characters long. However, TextProperty values are not indexed, and cannot be used in filters or sort orders
有一些替代方法可以做到这一点。
- 如果“文本”短于 500 个字符,那么您可以使用 StringProperty。 StringProperty可以在过滤器中使用。
- 尝试针对 Google 应用引擎的第 3 方全文搜索方法。 http://code.google.com/p/guestbook-example-appengine-full-text-search/
- 应用引擎团队正在努力在不久的将来提供全文搜索功能。 http://googleappengine.blogspot.com/2012/01/happy-birthday-high-replication.html
编辑
全文功能由Google App Engine团队开发: https://developers.google.com/appengine/docs/python/search/overview
关于google-app-engine:查询模型中的字符串属性不会检索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957910/