3

I'm using App Engine, SDK 1.6.3 with Python 2.7.

I've created a model like this:

class MyModel(db.Model):
    name = db.StringProperty()
    website = db.StringProperty()

I can iterate and see everything except the Key id's. For example, in the interactive shell I can run this:

from models import *
list = MyModel.all()
for p in list:
    print(p.name)

and it prints the name of every Entity. But when I run this:

from models import *
list = MyModel.all()
for p in list:
    print(p.key.id)  [or p.key.name or p.key.app]

I get an AttributeError:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 317, in post
    exec(compiled_code, globals())
  File "<string>", line 4, in <module>
AttributeError: 'function' object has no attribute 'id'

Can anyone please help me??

2 Answers 2

7

key() and id() are instance methods. Try with parenthesis:

   for p in list:
        print(p.key().id())

See the documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

key() is a method and id() is also a method.
So you'd need to do:

from models import MyModel
lst = MyModel.all()
for p in lst:
    print(p.key().id())

Other notes:

  1. Try to avoid, when possible, from [something] import *. This can cause difficult-to-debug namespace issues.
  2. Don't shadow built-ins with variable names. E.g. list should not be used.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.