What is a compatible version of this query for NDB?

This may be wrong, but I always use this query for my application:

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

What is a compatible version of this query for NDB? Metadata requests are very different.

edit : cid is an entity, and cme_only is an iterable, which I am 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 the template:

 {{ for Comput in cme_only }} 

I don't like it, but that was enough

+4
source share
2 answers

If cid is your entity, you can do this:

 from google.appengine.ext import ndb cme_only = ndb.Key(Comput, cid.key.id()).get() 

But this will return to you basically the same entity that you start with, cid , but overall this is one way of querying by key.

You can learn more about how to construct keys in documents.

+3
source

No need for metadata requests. The NDB method for requesting a request for __key__ as follows:

 ModelClass.query(ModelClass._key == key_value) 

Just as a request for the foo property is performed by filtering on ModelClass.foo == value , ModelClass._key is a pseudo-property representing the key.

Other posters are correct if you are only one object with a full key, using the get() method on a Key object is better (faster and cheaper). In addition, if e is an entity (an instance of the model), in NDB the key is not e.key() , but e.key (or e._key - yes, the same _key attribute that I mentioned above, it works like a class attribute and as an attribute of an instance).

Indeed, if you have a urlsafe key (for example, 'agFfcg4LEghFbXBsb3llZRgDDA' ), the way to turn it into a Key ndb.Key(urlsafe='agFfcg4LEghFbXBsb3llZRgDDA') object ndb.Key(urlsafe='agFfcg4LEghFbXBsb3llZRgDDA') .

Good luck

+7
source

All Articles