Skip to content Skip to sidebar Skip to footer

Pulling Data From Datastore And Converting It In Json In Python(google Appengine)

I am creating an apllication using google appengine, in which i am fetching a data from the website and storing it in my Database (Data store).Now whenever user hits my application

Solution 1:

If you are starting with a new app, I would suggest to use the NDB API rather than the old DB API. Your code would look almost the same though.

As far as I can tell from your code sample, the query should give you results as far as the HTTP query parameters from the request would match entity objects in the datastore.

I can think of some possible reasons for the empty result:

  • you only think the output is empty, because you use write() too early; app-engine doesn't support streaming of response, you must write everything in one go and you should do this after you queried the datastore
  • the properties you are filtering are not indexed (yet) in the datastore, at least not for the entities you were looking for
  • the filters are just not matching anything (check the log for the values you got from the request)
  • your query uses a namespace different from where the data was stored in (but this is unlikely if you haven't explicitly set namespaces anywhere)

In the Cloud Developer Console you can query your datastore and even apply filters, so you can see the results with-out writing actual code.

  1. Go to https://console.developers.google.com
  2. On the left side, select Storage > Cloud Datastore > Query
  3. Select the namespace (default should be fine)
  4. Select the kind "commoditydata"
  5. Add filters with example values you expect from the request and see how many results you get

Also look into Monitoring > Log which together with your logging.info() calls is really helpful to better understand what is going on during a request.

The conversion to JSON is rather easy, once you got your data. In your request handler, create an empty list of dictionaries. For each object you get from the query result: set the properties you want to send, define a key in the dict and set the value to the value you got from the datastore. At the end dump the dictionary as JSON string.

classMainHandler(webapp2.RequestHandler):
    defget(self):
        commodityname = self.request.get('veg')
        market = self.request.get('market')
        if commodityname isNoneand market isNone:
            # 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 )

Post a Comment for "Pulling Data From Datastore And Converting It In Json In Python(google Appengine)"