Albert
Albert

Reputation: 3651

Convert db.Key to ndb.Key - Python on AppEngine

It seems that db.Key and ndb.Key instances are not the same.

I have a db.Key instance. How do I convert it to an ndb.Key?

This is what I'm trying to do:

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.

Any ideas?

Upvotes: 1

Views: 710

Answers (2)

user1939565
user1939565

Reputation: 203

To convert DB key to NBD key you will have to :

ndb.Key.from_old_key(your_old_DB_key) 

Take a look at NDB Cheet Sheet for your further conversions.

Upvotes: 4

Thanos Makris
Thanos Makris

Reputation: 3115

You need to convert from the "old" db.Key to the new ndb.Key. Take a look at NDB Key Class for more information.

Upvotes: 3

Related Questions