exployre
exployre

Reputation: 67

TypeError: 'Key' object not callable

I am trying to call up a specific model of an entity in the google app store with this code in python:

class Profile(BlogHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            user = users.get_current_user()
            user_db_qry = User.query(User.theid == user.federated_identity())
            #theid is how I have saved each user's federated id in the User entity in the datastore
            user_db_list = user_db_qry.fetch(1)
            profile_user = user_db_list[0]
            profile_id = profile_user.key().id_or_name()
            #I am trying to get the datastore created ID/Name here and this is the line that gives me the error

            self.redirect('/profile/%s' % str(profile_id))
        else:
            self.redirect("/about")

So I am not quite sure what is going wrong. Is my query incorrect?

Upvotes: 0

Views: 3692

Answers (3)

Jeff Tratner
Jeff Tratner

Reputation: 17076

Based on what you've written other places, how have you been allocating keys for your entities? Are you giving them names instead?

If you are using a key path ending in something like this: (note: I don't mean parent, just key path)

ndb.Key.from_path(..."User", "harold")

for your keys, then the keys will return None for their id but should (if ndb works similarly to db) have a name attribute.

The easiest way to figure this out is to run your app in the SDK and go to http://localhost:8080/_ah/admin , open up the interactive console and try (possibly all) the properties until you get the one you want. With app engine, it often helps catch weird errors (for example, many of the API calls specifically fail if you try to use them with kewyord arguments instead of positional arguments, etc)

i'd also suggest trying to do a lookup from key you generate from ndb.Key.from_path() to a user instance and see if you are correctly instantiating/getting/whatever your keys.

Upvotes: 0

jdi
jdi

Reputation: 92569

To preface, I don't have any experience with GAE. This answer is based purely on the API docs

The docs claim that only model instances have a key() method which returns a Key class. Though, the User class seems to be its own kind of entity.

Instead of trying to access a key instance, you may just be able to use the direct methods:

profile_id = profile_user.user_id()
profile_nick = profile_user.nickname()

If you want to investigate what the key member of the user contains, you could debug by checking it out:

print type(profile_user.key)
print dir(profile_user.key)

Update

In your comments you clarified that you are using the NDB variants which explains the issue. The NDB Key Class is different from the DB Key Class. It has no id_or_name() method. What it has is:

id() Returns the string or integer id in the last (kind, id) pair, or None if the key is incomplete.

string_id() Returns the string id in the last (kind, id) pair, or None if the key has an integer id or is incomplete.

integer_id() Returns the integer id in the last (kind, id) pair, or None if the key has an string id or is incomplete.

Upvotes: 4

Amber
Amber

Reputation: 526613

        profile_id = profile_user.key().id_or_name()

doesn't need the first set of parens.

        profile_id = profile_user.key.id_or_name()

Upvotes: 2

Related Questions