0

Is it possible to select both the key as well as properties in an ndb query?

Right now I have:

one = ndb.gql('SELECT __key__ FROM Subject ORDER BY order ASC')
two = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC')

Is it possible to have something like:

oneandtwo = ndb.gql('SELECT __key__,name,order FROM Subject ORDER BY order ASC')
#This didn't work

If not, is it possible to combine one and two queries given that they are the same size?

oneandtwo = one + two
#This didn't work either

Thanks in advance!

mrmo123
  • 725
  • 1
  • 8
  • 23

1 Answers1

2

Have you read the docs on projection queries and tried it ? https://cloud.google.com/appengine/docs/standard/python/ndb/projectionqueries

Everything you need is in the documentation.

If you perform a projection query on "name" for instance you will get objects back that include the key and the single property "name", if you used name and order you would get both of those properties. In addition the object has the key. However projection queries won't work if the matching index has not been built.

 > x = ndb.gql("SELECT name from Product").fetch(5)
 > x
[<Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0001')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0002')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-25mm-BSP', 'Product', '002812-0002')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-25mm-BSP', 'Product', '002812-0003')"/>, <Product key_name="Key('Product', 'Seagull-Fibreglass-Spill', 'Product', '004321-0001')"/>]
 >  x[0]
<Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0001')"/>
 >  x[0].name
u'002811-0001'
 >  x[0].price
Traceback (most recent call last):
Ozy
  • 55
  • 1
  • 7
Tim Hoffman
  • 12,976
  • 1
  • 17
  • 29
  • If you a get() the you get a single entity. I am using fetch() in the example. access via index implies a list, and you haven't mentioned how you are using the list or fetching the results – Tim Hoffman May 21 '14 at 06:29
  • Thanks. I realized after I posted that I was testing the code in a forloop which is probably why indexing didn't work – mrmo123 May 21 '14 at 06:44
  • Yep each iteration will give you a single entity. – Tim Hoffman May 21 '14 at 06:58