简体   繁体   English

在 App Engine Python 中向 Google Datastore 实体动态添加属性(灵活环境)

[英]Add properties to Google Datastore entity dynamically in App Engine Python (Flexible environment)

Problem问题

  • I want to add properties to an existing entity like an Expando Class .我想向现有实体添加属性,例如Expando Class

Question

  • How to add properties to Google Datastore entity dynamically in App Engine Python (Flexible environment).如何在 App Engine Python(灵活环境)中向 Google Datastore 实体动态添加属性。

Development Environment开发环境

  • App Engine Python (Flexible environment). App Engine Python(灵活的环境)。
  • Python 3.6蟒蛇 3.6

Tried → Error尝试→错误

Best regards,此致,

This depends a little bit on how you want your application to work, but you can do something similar by using the Python Client for Google Cloud Datastore , which does work in Python3.X.这在一定程度上取决于您希望应用程序如何工作,但您可以使用适用于 Google Cloud DatastorePython 客户端来执行类似的操作,该客户端可在 Python3.X 中运行。

For example, you can update an already existing entity with this library by doing the following:例如,您可以通过执行以下操作使用此库更新现有实体:

from flask import Flask
from google.cloud import datastore


app = Flask(__name__)


@app.route('/')
def mainhandler():  
    client = datastore.Client()
    # Entity to update Kind and ID. Replace this values 
    # with ones that you know that exist.
    entity_kind = 'Foo-entity'
    entity_id = 123456789
    key = client.key(entity_kind, entity_id)

    entity = client.get(key)

    entity.update({
            "foo": u"bar",
            "year": 2019
        })
    client.put(entity)
    return "Entity updated"

As you can see, to update an existing entity you will need its kind and its unique ID (there is no other way around).如您所见,要更新现有实体,您将需要其种类和唯一 ID(别无他法)。

However, you don't need to do so when you are creating the Entity, and can just update it on the run once it has been created:但是,在创建实体时不需要这样做,并且可以在创建实体后在运行时更新它:

from flask import Flask
from google.cloud import datastore


app = Flask(__name__)


@app.route('/')
def mainhandler():  
    client = datastore.Client()
    entity_kind = 'Ticket-sale'
    key = client.key(entity_kind)

    ''' It is better to let Datastore create the Key 
    (unless you have some method to crate unique ID's), 
    so we call the allocate_ids method, and only keep the Key object'''
    key = client.allocate_ids(key, 1)[0]
    entity = datastore.Entity(key=key)
    entity.update({
            "foo": u"bar",
            "year": 20192
    })
    client.put(entity) # This creates the entity

    # update the entity once more with another property       
    entity.update({
            "month": u"January",
    })
    client.put(entity)
    return "Entity created"

Note that you will have to use the u"string" literal, in order to alert datastore that the string you are passing is encoded in unicode, otherwise it will show up as a string made up of random characters when accessing the property value.请注意,您必须使用u"string"文字,以提醒数据存储您传递的字符串是以 unicode 编码的,否则在访问属性值时它将显示为由随机字符组成的字符串。

As well, don't forget to update your requirements.txt file by adding the line google-cloud-datastore in order to import this library.同样,不要忘记通过添加google-cloud-datastore行来更新您的requirements.txt文件以导入此库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM