Michael Gradek
Michael Gradek

Reputation: 2738

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!

Upvotes: 5

Views: 1326

Answers (2)

Greg
Greg

Reputation: 10360

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])

Upvotes: 15

Peter Knego
Peter Knego

Reputation: 80330

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

Users.query(Users.user_email == email).get(keys_only=True)

Upvotes: 5

Related Questions