2011-10-12 90 views
0

不確定發生了什麼問題。我試圖查詢數據存儲中的簡單字符串,但查詢總是空白。無法使用篩選器在Google App Engine中查詢數據存儲區(Python)

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。

回答

2

TextProperty s沒有編入索引,也無法過濾。如果您需要索引,請使用StringProperty

+1

另外請注意,切換完這個之後,您需要獲取和'.put()'所有實體以便將它們編入索引。 –

相關問題