Pregunta

I am using google app engine with python. the server got the entity key from the user, then I use this code to bring the entity:

 key.get()

but I also want to get the entity just if it related to particular model, How could I do that ? I know that I could do that by this code:

MyModel.get_by_id('my_key')

but this works just for the key_name and the id , and in my case I use the key ?

¿Fue útil?

Solución

After getting user-provided key as urlsafe string from server, construct the NDB key, e.g.:

key = ndb.Key(urlsafe=string)

I'm not sure though, why you don't simply use key.get() after importing MyModel :-)

However, this is how you get an instance using its ID (whether string or integer):

MyModel.get_by_id(key.id(), parent=key.parent(), app=key.app(), namespace=key.namespace())

The keywords are optional, unless you use multiple namespaces or application IDs, or MyModel is child class in an entity group.

Alternatively, use key.string_id() or key.integer_id()

Security warning: Since your app accepts user-provided keys, be aware that even the cryptically looking URL-safe keys can be easily encoded/decoded.

For details see Reference for NDB Key

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top