我以非常简单的方式使用Google Cloud数据存储区,并尝试按其ID检索实体。我读过this(它是用Java编写的,但似乎遵循相同的逻辑)
我的实体的Def在这里:
class Logs(ndb.Model):
startDate = ndb.DateTimeProperty()
endDate = ndb.DateTimeProperty()
requestedDate = ndb.DateProperty()
taskName = ndb.StringProperty()
status = ndb.StringProperty()
然后,当我插入新的
时logs = Logs(startDate=datetime.utcnow(),
taskName=taskName,
requestedDate=requestedDate,
status=u'IN_PROGRESS')
key = logs.put()
id = key.id() # I use this variable later
当我想要检索它时
logs = Logs.get_by_id(id)
但它永远不会返回任何实体......
这有什么问题?
感谢您的帮助
答案 0 :(得分:3)
根据documentation,你应该能够直接从Key对象调用get()来从Datastore中检索实体:
logs_entity = Logs(startDate=datetime.utcnow(),
taskName=taskName,
requestedDate=requestedDate,
status=u'IN_PROGRESS')
# Saves entity to Datastore and returns Key
entity_key = logs_entity.put()
# Retrieves entity from Datastore using the previous Key
result = entity_key.get()
修改强>
如果您需要将密钥作为字符串传递以稍后重建Key对象,可以尝试使用urlsafe()方法,该方法允许将其嵌入到URL中:
urlsafe_string = entity_key.urlsafe()
[...]
entity_key= ndb.Key(urlsafe=urlsafe_string)
logs_entity = entity_key.get()