This is a bit late, however it will help anyone who has the same problem.
The solution is to first execute the query only for keys and take a subset of the keys that are direct descendants.
With this subset of keys, you can batch get the right objects.
I am not familiar with python, so here is an example in go:
directDescKeys := make([]*datastore.Key, 0) q := datastore.NewQuery("A").Ancestor(parentKey).KeysOnly() for it := q.Run(ctx);; { key, err := it.Next(nil) if err == datastore.Done { break } else if err != nil { // handle error } if reflect.DeepEquals(key.Parent(), parentKey) { directDescKeys = append(directDescKeys, key) } } entities := make([]*A, len(directDescKeys)) if err := datastore.GetMulti(ctx, directDescKeys, entities); err != nil { // handle error }
source share