Google app engine can';t更新谷歌应用程序引擎中的数据存储
我创建了一个类似于guestbook教程的数据存储对象:Google app engine can';t更新谷歌应用程序引擎中的数据存储,google-app-engine,google-apps-script,google-cloud-datastore,datastore,google-app-engine-python,Google App Engine,Google Apps Script,Google Cloud Datastore,Datastore,Google App Engine Python,我创建了一个类似于guestbook教程的数据存储对象: class myDS(ndb.Model): a = ndb.StringProperty(indexed=True) 我有一个处理程序来访问它,更新是: class Handler1: my_ds = myDS() my_ds.a = "abc" #Trying to update the value class Handler2: my_ds = myDS() self.response.
class myDS(ndb.Model):
a = ndb.StringProperty(indexed=True)
我有一个处理程序来访问它,更新是:
class Handler1:
my_ds = myDS()
my_ds.a = "abc" #Trying to update the value
class Handler2:
my_ds = myDS()
self.response.write(my_ds.a) #Trying to access my_ds.aafter it was updated in Handlers1
def main():
application = webapp.WSGIApplication([
('/set', Handler1),
('/get', Handler2])
我呼吁:
Myapp.com/set
Myapp.com/get : Prints None (Didn't update to "abc")
为什么一个项目的价值没有更新?
如何跨处理程序进行更新?首先要注意的是,在
Handler1
中,您的代码实际上并没有将实体保存到数据存储中,因此您需要调用.put()
方法:
my_ds = myDS()
my_ds.a = "abc" #Trying to update the value
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
第二点需要注意的是,在Handler2
中,my_ds=myDS()
调用并不像您所期望的那样从数据存储中检索实体,它只是创建了一个新实体(它也不会保存到数据存储中)。要从数据存储中检索实体,需要按实体的键进行查找(或通过查询获取):
这些是关于使用数据存储的非常基本的概念,您可能需要更加熟悉它们。您应该浏览(可能还有其他相关章节,分组在文档页面左侧导航栏的googleclouddatastore
部分下)
最后,为了能够进行这种查找,您需要能够以某种方式确定在Handler1
中获得的实体密钥,或者将其传输到Handler2
,因为对这些处理程序的每个请求都是相互独立的。可能感兴趣的是:
通过webapp2
会话传递键的字符串表示的示例:
在Handler1中
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
self.session['urlsafe'] = my_ds_key.urlsafe()
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
self.redirect('/get?urlsafe=%s' % my_ds_key.urlsafe())
在Handler2中
urlsafe = self.session.get('urlsafe')
if urlsafe:
my_ds = ndb.Key(urlsafe=urlsafe).get()
urlsafe = self.request.get('urlsafe')
if urlsafe:
my_ds = ndb.Key(urlsafe=urlsafe).get()
使用URL查询字符串(如
/get?urlsafe=传递键的字符串表示形式的示例首先要注意的是,在Handler1
中,您的代码实际上并没有将实体保存到数据存储中,因此需要调用.put()
方法:
my_ds = myDS()
my_ds.a = "abc" #Trying to update the value
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
第二点需要注意的是,在Handler2
中,my_ds=myDS()
调用并不像您所期望的那样从数据存储中检索实体,它只是创建了一个新实体(它也不会保存到数据存储中)。要从数据存储中检索实体,需要按实体的键进行查找(或通过查询获取):
这些是关于使用数据存储的非常基本的概念,您可能需要更加熟悉它们。您应该浏览(可能还有其他相关章节,分组在文档页面左侧导航栏的googleclouddatastore
部分下)
最后,为了能够进行这种查找,您需要能够以某种方式确定在Handler1
中获得的实体密钥,或者将其传输到Handler2
,因为对这些处理程序的每个请求都是相互独立的。可能感兴趣的是:
通过webapp2
会话传递键的字符串表示的示例:
在Handler1中
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
self.session['urlsafe'] = my_ds_key.urlsafe()
my_ds_key = my_ds.put() # save my_ds to the datastore and get its key
self.redirect('/get?urlsafe=%s' % my_ds_key.urlsafe())
在Handler2中
urlsafe = self.session.get('urlsafe')
if urlsafe:
my_ds = ndb.Key(urlsafe=urlsafe).get()
urlsafe = self.request.get('urlsafe')
if urlsafe:
my_ds = ndb.Key(urlsafe=urlsafe).get()
使用URL查询字符串(如/get?urlsafe=谢谢)传递键的字符串表示的示例,有一件事我不明白-如何将Handler2调用到我在Handler1中编辑的特定实体?请参阅我在上一段中提到的帖子。我看到了帖子,但这还不够-即使我使用put()在Handler1中,我需要将钥匙放在Handler2中-但是Handler2无法从Handler1中识别钥匙,无法理解如何克服此问题谢谢,有一件事我不明白-我如何调用Handler2到我在Handler1中编辑的特定实体?请参阅我在上一段中提到的帖子。我看到了帖子,但这还不够-即使我在Handler1中使用put(),我也需要在Handler2中包含密钥-但Handler2无法从Handler1中知道密钥,我不明白如何克服这个问题