Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the compatible version of this query for NDB?

Maybe it's wrong but I always use this query for my app:

cme_only = Comput.all().filter('__key__ =', cid.key())

What is the compatible version of this query for NDB? The Metadata queries are very different..

edit: cid is an entity and cme_only is an iterable that I'm sure has only one value

cid = Comput.get_by_id(int(self.request.get('id')))
cme_only = Comput.all().filter('__key__ =', cid.key())

and then in template:

{{ for Comput in cme_only }}

I do not like it but it was enough

like image 726
Gianni Di Noia Avatar asked Dec 27 '22 22:12

Gianni Di Noia


1 Answers

There's no need for metadata queries. The NDB way to spell a query on __key__ is as follows:

ModelClass.query(ModelClass._key == key_value)

That is, just like querying for property foo is done by filtering on ModelClass.foo == value, ModelClass._key is a pseudo-property representing the key.

The other posters are correct that if you just one a single entity given its full key, using the get() method on the Key object is better (faster and cheaper). Also, if e is an entity (model instance), in NDB, the key is not e.key() but e.key (or e._key -- yes, that's the same _key attribute I mentioned in above, it works as a class attribute and as an instance attribute).

And indeed, if you have a urlsafe key (e.g. 'agFfcg4LEghFbXBsb3llZRgDDA') the way to turn it into a Key object is ndb.Key(urlsafe='agFfcg4LEghFbXBsb3llZRgDDA').

Good luck!

like image 134
Guido van Rossum Avatar answered Jan 28 '23 14:01

Guido van Rossum