0
votes

I have a couple of doubts regarding how NDB projection queries work and how the caching behaves behind the scenes

So given a model similar to:

class Users(ndb.Model):
    user_name = ndb.StringProperty(required=True)
    user_email = ndb.StringProperty(required=True)
    user_password = ndb.StringProperty(required=True)

    @classmethod # THIS ONE DOES NOT WORK
    def get_profile_info(cls, id):
        return ndb.Key(Users, id).get(projection=[Users.user_name])

    @classmethod # THIS ONE WORKS
    def get_profile_info(cls, id):
        return Users.query(Users.key == ndb.Key(Users, id)).get(projection=[Users.user_name])

Why does the first classmethod raise a "TypeError: Unknown configuration option ('projection')"? Can't I simply call a projection on a direct get of a key, instead of having to query for a key?

Secondly, regarding caching, I'm not sure if I correctly understood this thread: NDB Caching When Using Projected Queries

Aren't projected queries cached? Does this mean its better to simply call a get() (and fetch the whole instance) so it is cached, instead of projecting?

Thanks in advance!

1

1 Answers

2
votes

As per the error a projection makes no sense when using get. From the docs " It only gets values for those properties in the projection. It gets this data from the query index (and thus, properties in the projection must be indexed)". So doing get isn't accessing the object properties via indexes. Note gvr's comment on caching in your referenced question.