Python 2.7 从数据存储中提取数据并将其转换为python中的Json(Google Appengine)

Python 2.7 从数据存储中提取数据并将其转换为python中的Json(Google Appengine),python-2.7,google-app-engine,Python 2.7,Google App Engine,我正在使用google appengine创建一个应用程序,在其中我从网站获取数据并将其存储在我的数据库(数据存储)中。现在,每当用户点击我的应用程序url时,都会显示“application_url\name=xyz&city=abc”,我正在从数据库中获取数据,并希望将其显示为json。现在,我正在使用过滤器根据名称和城市获取数据,但输出为[]。我不知道如何从中获取数据。我的代码如下所示: class MainHandler(webapp2.RequestHandler): def

我正在使用google appengine创建一个应用程序,在其中我从网站获取数据并将其存储在我的数据库(数据存储)中。现在,每当用户点击我的应用程序url时,都会显示“application_url\name=xyz&city=abc”,我正在从数据库中获取数据,并希望将其显示为json。现在,我正在使用过滤器根据名称和城市获取数据,但输出为[]。我不知道如何从中获取数据。我的代码如下所示:

class MainHandler(webapp2.RequestHandler):
    def get(self):
        commodityname = self.request.get('veg',"Not supplied")
        market = self.request.get('market',"No market found with this name")
        self.response.write(commodityname)
        self.response.write(market)
        query = commoditydata.all()
        logging.info(commodityname)
        query.filter('commodity = ', commodityname)
        result = query.fetch(limit = 1)
        logging.info(result)
“commoditydata”表的数据库结构为


有谁能告诉我如何使用name and market从db获取数据,并将其转换为Json。首先从db获取数据更为优先。任何建议都将非常有用。

如果您开始使用新应用程序,我建议使用db API,而不是旧的db API。但是,您的代码看起来几乎相同

从您的代码示例中可以看出,只要请求中的HTTP查询参数与数据存储中的实体对象匹配,查询就会给出结果

我可以想出一些可能的原因来解释这个空洞的结果:

  • 您只认为输出是空的,因为您过早地使用了
    write()
    ;app engine不支持响应流,您必须一次性写入所有内容,并且应在查询数据存储后执行此操作
  • 您正在筛选的属性尚未在数据存储中建立索引,至少没有为您正在查找的实体建立索引
  • 过滤器只是不匹配任何内容(检查日志中从请求中获得的值)
  • 您的查询使用的名称空间与存储数据的名称空间不同(但如果您没有在任何地方显式设置名称空间,则不太可能这样做)
在CloudDeveloper控制台中,您可以查询数据存储,甚至应用过滤器,这样您就可以在不编写实际代码的情况下查看结果

  • 在左侧,选择存储>云数据存储>查询
  • 选择名称空间(默认值应该可以)
  • 选择种类“commoditydata”
  • 添加带有您期望从请求中得到的示例值的过滤器,并查看您得到了多少结果
  • 还要查看Monitoring>Log,它与
    logging.info()
    调用一起非常有助于更好地了解请求过程中发生的情况

    一旦获得数据,转换为JSON就相当容易了。在请求处理程序中,创建字典的空列表。对于从查询结果中获得的每个对象:设置要发送的属性,在dict中定义一个键,并将值设置为从数据存储中获得的值。最后,将字典转储为JSON字符串

    class MainHandler(webapp2.RequestHandler):
        def get(self):
            commodityname = self.request.get('veg')
            market = self.request.get('market')
            if commodityname is None and market is None:
                # the request will be complete after this:
                self.response.out.write("Please supply filters!")
            # everything ok, try query:
            query = commoditydata.all()
            logging.info(commodityname)
            query.filter('commodity = ', commodityname)
            result = query.fetch(limit = 1)
            logging.info(result)
            # now build the JSON payload for the response
            dicts = []
            for match in result:
                dicts.append({'market': match.market, 'reporteddate': match.reporteddate})
            # set the appropriate header of the response:
            self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
            # convert everything into a JSON string    
            import json
            jsonString = json.dumps(dicts)
            self.response.out.write( jsonString )
    

    非常感谢,安尼
    class MainHandler(webapp2.RequestHandler):
        def get(self):
            commodityname = self.request.get('veg')
            market = self.request.get('market')
            if commodityname is None and market is None:
                # the request will be complete after this:
                self.response.out.write("Please supply filters!")
            # everything ok, try query:
            query = commoditydata.all()
            logging.info(commodityname)
            query.filter('commodity = ', commodityname)
            result = query.fetch(limit = 1)
            logging.info(result)
            # now build the JSON payload for the response
            dicts = []
            for match in result:
                dicts.append({'market': match.market, 'reporteddate': match.reporteddate})
            # set the appropriate header of the response:
            self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
            # convert everything into a JSON string    
            import json
            jsonString = json.dumps(dicts)
            self.response.out.write( jsonString )