Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDB projection of instance Key or ID

I'm using NDB on GoogleAppEngine and I want to retrieve a instance Key or ID by passing an e-mail into the query.

My Model looks something like this:

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

    @classmethod
    def get_password_by_email(cls, email):
        return Users.query(Users.user_email == email).get(projection=[Users.key, Users.user_password])

When running the code, I get the following error:

BadProjectionError: Projecting on unknown property __key__

How can I get an instance ID or Key by querying users through an e-mail in AppEngine's NDB (e.g. Login process)?

Thanks!

like image 430
Michael Gradek Avatar asked Sep 02 '12 17:09

Michael Gradek


2 Answers

A projection query will always include the key as well as the fields you specify, so if keys_only isn't sufficient, then:

return Users.query(Users.user_email == email).get(projection=[Users.password])
like image 153
Greg Avatar answered Oct 22 '22 23:10

Greg


If you only need Key you can try keys-only query:

Users.query(Users.user_email == email).get(keys_only=True)
like image 5
Peter Knego Avatar answered Oct 22 '22 21:10

Peter Knego