class Thread(db.Model):
members = db.StringListProperty()
def user_is_member(self, user):
return str(user) in self.members
和
thread = Thread.get(db.Key.from_path('Thread', int(id)))
is_member = thread.user_is_member(user)
但错误是:
Traceback (most recent call last):
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
handler.get(*groups)
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 62, in check_login
handler_method(self, *args)
File "D:\zjm_code\forum_blog_gae\main.py", line 222, in get
is_member = thread.user_is_member(user)
AttributeError: 'NoneType' object has no attribute 'user_is_member'
为什么?
感谢
答案 0 :(得分:6)
您正在尝试按键获取实体,但不存在具有该键的实体,因此.get()返回None。在尝试对其执行操作之前,您需要检查是否已返回有效实体,如下所示:
thread = Thread.get(db.Key.from_path('Thread', int(id)))
if thread:
is_member = thread.user_is_member(user)
else:
is_member = False
或等同于:
thread = Thread.get(db.Key.from_path('Thread', int(id)))
is_member = thread.user_is_member(user) if thread else False