Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting key from the id of the entity using Appengine's NDB

I have a model based on ndb, while saving it, I stored 'id' field with current logged-in user's user id. (Why I am doing this? Actually this model used to be based on db.Model and key_name has this user's id. Now, I am converting it to ndb)

m= Modelclass(id = str(users.get_current_user().used_id()),
      --- Other fields ---
m.put()

This model's edit form sends this 'id' and I wanted to get corresponding 'key' from it. But, I got "Key id number is too long; received 'some big_number'". Tried both ways

Modelclass.get_by_id(<id>).key 

OR

ndb.Key('Modelclass', <id>)

This is one case, there may be other cases where user can store some big number in 'id' field. In these scenarios, we can't extract key from 'id'. So, how to solve such a problem.

I am new to ndb. Thanks for any help.

like image 260
rajpy Avatar asked Dec 05 '25 17:12

rajpy


1 Answers

Looks like your value is an int, not a string. But you converted it into a string when creating the entitiy. There's a simple solution:

ndb.Key('Modelclass', str(<id>))

Good luck!

like image 112
Guido van Rossum Avatar answered Dec 08 '25 10:12

Guido van Rossum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!