【问题标题】:GCP: Fetch the primary key field along with other fields in DatastoreGCP:获取主键字段以及数据存储区中的其他字段
【发布时间】:2022-01-26 10:06:56
【问题描述】:

我想从数据存储类型中获取所有列。

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)

当前获取以下响应作为输出,但我想获取数据存储中也存在的 id 作为主键

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

【问题讨论】:

标签: python-3.x google-cloud-platform google-cloud-datastore datastore


【解决方案1】:

我用下面的代码解决了这个问题。

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)
        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2019-06-22
    • 2016-02-20
    • 2022-12-14
    • 2019-03-11
    • 1970-01-01
    • 2017-09-29
    相关资源
    最近更新 更多