将db.Key转换为ndb.Key - AppEngine上的Python

时间:2012-12-06 09:58:31

标签: google-app-engine google-cloud-datastore app-engine-ndb

似乎db.Key和ndb.Key实例不一样。

我有一个db.Key实例。如何将其转换为ndb.Key?

这就是我要做的事情:

NDBEntity(ndb.Model):
  some_property = ndb.StringProperty()

DBEntity(db.Model):
  some_property = ndb.StringProperty()

# I have an instance of a DBEntity already saved in the datastore
db_entity_instance = DBEntity.all().get()

ndb_entity_instance = NDBEntity(id="some_id", parent=db_entity_instance.key(), some_property="foo").put()
# The above line doesn't work because it expects a Key Instance for the parent, and it doesn't seem to recognize a db.Key instance.

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

要将DB密钥转换为NBD密钥,您必须:

ndb.Key.from_old_key(your_old_DB_key) 

请查看NDB Cheet Sheet,了解您的进一步转化情况。

答案 1 :(得分:3)

您需要将“旧”db.Key转换为新的ndb.Key。有关详细信息,请查看NDB Key Class