Get only projected properties of an entity


How can I get only entity's properties specified in the projection query in the entity's _properties list?

I mean:

class Demo(ndb.Model):
    first_prop = ndb.StringProperty()
    second_prop = ndb.StringProperty()

Demo( first_prop='First', second_prop='Second' ).put()

q = Demo.query( projection=[first_prop] )
e = q.fetch()
print e[0]._properties.keys()

returns ['second_prop', 'first_prop']. I want len(_properties) to be 1...



There is a _projection property you can use on the result (invoke this handler by loading it in your browser http://localhost:8080/projection at least twice) :

import webapp2
from google.appengine.ext import ndb


class Dummy(ndb.Model):
    p1 = ndb.StringProperty()
    p2 = ndb.StringProperty()


class ProjectionHandler(webapp2.RequestHandler):

    def get(self):
        # run this handler at least twice before looking at the console output
        d = Dummy(id='abc')
        d.p1 = 'p1'
        d.p2 = 'p2'
        d.put()
        q = Dummy.query(projection=['p1'])
        r = q.fetch()
        if len(r) > 0:
            print r[0]._properties.keys()  # prints: ['p1', 'p2']
            print r[0]._projection  # prints: ('p1',)

app = webapp2.WSGIApplication([
    ('/projection', ProjectionHandler)
])

Additionally,

q.projection returns (Demo('first_prop'),)

Is it possible that instead of the name of the property as a string i.e. 'first_prop' you passed the entity or some other object in Demo.query( projection=[first_prop] )? You should get the same result as r[0]._projection.