Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于变量指定GAE Python ndb字段名_Python_Google App Engine_Google Cloud Datastore_App Engine Ndb - Fatal编程技术网

如何基于变量指定GAE Python ndb字段名

如何基于变量指定GAE Python ndb字段名,python,google-app-engine,google-cloud-datastore,app-engine-ndb,Python,Google App Engine,Google Cloud Datastore,App Engine Ndb,在将数据存储到NDB中时,我希望从字符串变量(例如field_name2=“userid”)中获取字段的名称。有办法吗?提前谢谢 class Account(ndb.Model): username = ndb.StringProperty() userid = ndb.IntegerProperty() class MainPage(): field_name2 = 'userid' acct = Account.get_key(id).get()

在将数据存储到NDB中时,我希望从字符串变量(例如field_name2=“userid”)中获取字段的名称。有办法吗?提前谢谢

class Account(ndb.Model):
     username = ndb.StringProperty()
     userid = ndb.IntegerProperty()

class MainPage():
    field_name2 = 'userid'

    acct = Account.get_key(id).get() 

    acct.username = "Bob"
    acct[**field_name2**] = "001"  ## How can I do something like this?
    acct.put()

由于
ndb.Models
的行为类似于普通的Python对象,因此可以使用Python和

在您的示例中:

    setattr(acct, field_name2, "001")
试一试 另见: