【问题标题】:Can't get filter to work for querying datastore in Google App Engine (Python)无法让过滤器在 Google App Engine (Python) 中查询数据存储区
【发布时间】:2011-10-13 07:28:55
【问题描述】:

不知道出了什么问题。我正在尝试在数据存储中查询一个简单的字符串,但查询结果总是空的。

models.py:

from google.appengine.ext import db
from google.appengine.api import datastore_types

class Post(db.Model):
    author = db.TextProperty()
    title = db.TextProperty()
    slug = db.TextProperty()
    #slug = str()

main.py:

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from models import *
import logging
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')
from django.template.defaultfilters import slugify


class MainHandler(webapp.RequestHandler):
    def get(self):

        # store some random posts
        title1 = "Road to the perfect car wax."
        title2 = "10 ways to get a perfect car wax."
        slug1 = slugify(title1)
        slug2 = slugify(title2)

        post1 = Post(author="Tom Tomton", title=title1, slug=str(slug1))
        post2 = Post(author="John Jonston", title=title2, slug=str(slug2))

        posts = list()
        posts.append(post1)
        posts.append(post2)
        db.put(posts)

        # use filter to find a post
        q = Post.all()
        q.filter("slug =",str(slug1))
        #q.filter("slug =","road-to-the-perfect-car-wax")
        result = q.fetch(1)[0] # empty!
        logging.info("result: %s" % result) 

        self.response.out.write('<p class="item">%s, %s</p>' % (result.author, result.title))


application = webapp.WSGIApplication([('/.*', MainHandler)], debug=True)


def main():
    wsgiref.handlers.CGIHandler().run(application)
    #util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

数据存储查看器中的输出清楚地显示它就在那里:

2   1   None    Tom Tomton  road-to-the-perfect-car-wax     Road to the perfect car wax.
2   2   None    John Jonston    10-ways-to-get-a-perfect-car-wax    10 ways to get a perfect car wax.

我尝试过使用应用引擎版本 1.5.5 和 1.5.0。

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    TextPropertys 未编入索引,也无法过滤。如果需要索引,请使用 StringProperty

    【讨论】:

    • 另请注意,在切换此设置后,您需要获取并.put()所有实体才能对其进行索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2013-05-17
    • 1970-01-01
    • 2015-12-22
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多