Projection of an NDB instance of a key or identifier

I use NDB in GoogleAppEngine and I want to get the key or instance id by passing the message to the request.

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 I run the code, I get the following error:

BadProjectionError: Projecting on unknown property __key__

How can I get the identifier or instance key by querying users via email in AppEngine NDB (e.g. login process)?

Thank!

+5
source share
2 answers

The projection request will always indicate the key, as well as the fields you specify, so if keys_only is not enough, then:

return Users.query(Users.user_email == email).get(projection=[Users.password])
+13
source

If you need only the key, you can try the request only for the key:

Users.query(Users.user_email == email).get(keys_only=True)
+5
source

All Articles