Search code examples
python-3.xgoogle-cloud-platformgoogle-cloud-datastoredatastore

GCP: Fetch the primary key field along with other fields in Datastore


I would like to fetch all the column from the datatstore kind.

datastore fields

#To get all books(all entity) from the datastore
@app.route("/getbookdetails", methods=['GET'])
  def getbookdetails():
  query=client.query(kind='tableofbooks')
  results = list(query.fetch())
  return json.dumps(results)

currently getting the below response as output but I would like to fetch the id which is also present in the datastore as the primary key

[
    {
        "author": "new author",
        "description": "Google Datastore",
        "title": "sample insert",
        "yearofpublication": 2021
    }]

Solution

  • I fixed the issue with the code below.

    query=client.query(kind='tableofbooks')
            mylist=query.fetch()
            bookarray=[]
            for x in mylist:
                thisdict ={
                     "id":x.id,
                     "author":x['author'],
                     "description":x['description'],
                     "title":x['title'],
                     "yearofpublication":x['yearofpublication'],
                }
                bookarray.append(thisdict)
            return json.dumps(bookarray)