【发布时间】:2012-12-06 09:58:30
【问题描述】:
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.
有什么想法吗?
【问题讨论】:
-
在同一个应用程序中使用这两个可能是个坏主意 - 您应该一次移植所有代码。
-
我同意 Nick 的观点,但在某些情况下,将旧样式模型转换为新模型可能会非常昂贵。在这种情况下,可能需要临时在新旧键之间工作,或者在新 ndb 模型上创建对旧 db.py 模型的属性引用。
标签: google-app-engine google-cloud-datastore app-engine-ndb