1
votes

I would like to write an ndb query which excludes a particular entity, identified by its id, from the result set.

I have tried the following:

result = Entity.query(Entity.key.id() != 'entity-id', *some other condition*).fetch()

The Entity.key.id() != 'entity-id' bit throws an error. What is the correct syntax?

2

2 Answers

2
votes

I don't know if its possible to query exclude on key, you have a simple workaround to remove the result from the final results.

But if it worked the syntax should be something like this:

result = Entity.query(Entity.key != ndb.Key(Entity, id), *some other condition*).fetch()
0
votes

It is possible to exclude a particular entity based on its id using the db API (and I presume the ndb API with some minor alterations). The following syntax works for me using the db API. It builds a query with results that exclude the id in the id_to_retrieve variable:

id_to_retrieve = 123456
query = Entity.all() 
query.filter('__key__ !=',  db.Key.from_path('Entity', id_to_retrieve))
query.fetch(None)