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
如何使用google appengine python ndb更新数据?_Python_Google App Engine - Fatal编程技术网

如何使用google appengine python ndb更新数据?

如何使用google appengine python ndb更新数据?,python,google-app-engine,Python,Google App Engine,我有一个用户表,有两个字段。我想每天更新一个字段。我该怎么做? 我的代码: class User(ndb.Model): username = ndb.StringProperty() something = ndb.StringProperty(repeated=True) created_date = ndb.DateTimeProperty(auto_now_add=True) updated_date = ndb.DateTimeProperty(auto

我有一个用户表,有两个字段。我想每天更新一个字段。我该怎么做? 我的代码:

class User(ndb.Model):
    username = ndb.StringProperty()
    something = ndb.StringProperty(repeated=True)
    created_date = ndb.DateTimeProperty(auto_now_add=True)
    updated_date = ndb.DateTimeProperty(auto_now_add=True)

我想更改某个字段,但如何更改?

如果您指的是手动而不是自动,请如下所示:

#Assuming you already know the key of the entity you want to change. 

user = user_key.get()        
user.something = 'new something'    
user.put()
for user in User.query():
    user.updated_date = datetime.datetime.now()
    user.put()
如果要查看所有
User
对象,可以执行以下操作:

#Assuming you already know the key of the entity you want to change. 

user = user_key.get()        
user.something = 'new something'    
user.put()
for user in User.query():
    user.updated_date = datetime.datetime.now()
    user.put()
这相当慢,但只要用户不多,它就能完成任务。
如果您想每天更新所有
User
对象,您可能需要查看和。

嗯,您需要知道要更新哪个实体,对吗?如果您知道实体,请直接使用它,而不是从键获取它。如何获取user=user\u key.get():要检索由给定键标识的实体,请将key对象作为参数传递给db.get()函数。您可以使用类方法Key.from_path()生成Key对象。完整路径是祖先路径中的一系列实体,每个实体由其种类(一个字符串)表示,后跟其标识符(键名或数字ID)。您需要改变对数据存储的看法,您没有表,您有实体,获取单个实体并更新实体的属性,然后写入(放置)实体。