2012-03-22 106 views
2

我正在使用App Engine,SDK 1.6.3和Python 2.7。App Engine - AttributeError:'function'object has no attribute'id'

我创建了一个模型是这样的:

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

我可以遍历,看看除了密钥ID的一切。例如,在交互式shell中,我可以运行以下代码:

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

并打印每个实体的名称。但是,当我运行此:

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

我得到一个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' 

谁能帮我?

回答

7

key()和id()是实例方法。试用括号:

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

查看documentation

+0

太棒了;谢谢!我简直不敢相信那么简单...... – 2012-03-22 01:36:30

1

key() is a methodid() is also a method
所以你需要做的:

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

其他说明:

  1. 尽量避免,可能的情况下,from [something] import *。这会导致难以调试的命名空间问题。
  2. 不要使用变量名称来影射内置插件。例如。不应使用list
相关问题