gae python数据存储区查询

时间:2012-03-14 05:51:26

标签: python google-app-engine datastore

我也是gae和python的新手,我正在尝试使用数据存储区构建简单的应用程序,其中脚本如下所示

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class Pincodes(db.Model):
  city = db.StringProperty()
  code = db.StringProperty()

class MainHandler(webapp.RequestHandler):
  def get(self):
    q = Pincodes.all()
    q = q.filter("city =", "some_city")
    p = q.get()
    r = 'city: %s code: %s' % (pincode.city, pincode.code)
    self.response.out.write(r)     

我的脚本还包含通常的def main()和if__name,我从code.google文档中显示的简单hello world应用程序开始逐步开发它并且工作正常,我已经将包含10条记录的示例密码数据上传到本地数据存储区它也很好,但我无法在网页上查询和显示它我试过self.response.out.write,输出是“city:code:”而不是“city:mumbai code:400001”我的脚本错误

1 个答案:

答案 0 :(得分:1)

使用

entity = q.get() # use get if you want one entity only  
r = 'city: %s code: %s' %(entity.city, entity.code)  
self.response.out.write(r)

而不是打印

编辑:

 def get(self):
    q = Pincodes.all()
    q = q.filter("city =", "some_city")
    entity = q.get() # use get if you want one entity only  
    r = 'city: %s code: %s' %(entity.city, entity.code)  
    self.response.out.write(r)

EDIT2:

 def get(self):
    q = Pincodes.all()
    q = q.filter("city =", "some_city")
    entity = q.get() # use get if you want one entity only  
    if not entity:
        self.response.out.write('sorry no entities found')
    else:
        r = 'city: %s code: %s' %(entity.city, entity.code)  
        self.response.out.write(r)