NoneType没有属性Append

时间:2010-04-07 20:34:56

标签: python google-app-engine syntax

我是Python的新手。我无法理解为什么变量在我的代码中的某个点上是None

class UsersInRoom(webapp.RequestHandler):
    def get(self):
        room_id = self.request.get("room_id")
        username = self.request.get("username")
        UserInRoom_entities = UserInRoom.gql("WHERE room = :1", room_id).get()
        if UserInRoom_entities:
            for user_in_room in UserInRoom_entities:
                if user_in_room.username == username:
                    user_in_room.put() # last_poll auto updates to now whenenever user_in_room is saved
        else:
            user_in_room = UserInRoom()
            user_in_room.username = username
            user_in_room.put()
            // error here, on line 160
            UserInRoom_entities = []
            UserInRoom_entities.append(user_in_room)

        # name is `user_at_room` intead of `user_in_room` to avoid confusion    
        usernames = [user_at_room.username for user_at_room in UserInRoom_entities]
        self.response.out.write(json.dumps(usernames))

错误是:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 507, in __call__
    handler.get(*groups)
  File "path\to\chat.py", line 160, in get
AttributeError: 'NoneType' object has no attribute 'append'

这怎么可能?我在电话会议之前立即设置了UserInRoom_entities = []。或者其他问题是None

更新:此代码有效:

class UsersInRoom(webapp.RequestHandler):
    def get(self):
        room_id = self.request.get("room_id")
        username = self.request.get("username")
        UserInRoom_entities = UserInRoom.gql("WHERE room = :1", room_id).get()
        if UserInRoom_entities:
            for user_in_room in UserInRoom_entities:
                if user_in_room.name == username:
                    user_in_room.put() # last_modified auto updates to now whenenever user_in_room is saved
        else:
            user_in_room = UserInRoom(room=Key(room_id), name=username)
            user_in_room.put()

            UserInRoom_entities = []
            UserInRoom_entities.append(user_in_room)

        # name is `user_at_room` intead of `user_in_room` to avoid confusion    
        usernames = [user_at_room.name for user_at_room in UserInRoom_entities]
        self.response.out.write(json.dumps(usernames))

class ChatRoom(db.Model):
    name = db.StringProperty()
    last_modified = db.DateTimeProperty(auto_now=True)
    message_contents = db.StringListProperty()
    message_users = db.StringListProperty()

class UserInRoom(db.Model):
    room = db.ReferenceProperty(ChatRoom)
    name = db.StringProperty()
    last_modified = db.DateTimeProperty(auto_now=True)

2 个答案:

答案 0 :(得分:3)

由于看起来我对这个问题的评论有了答案,我会重复一遍作为答案,希望获得一些声望点:

UserInRoom实例是否已正确初始化?我不熟悉GAE数据模型,但我可以想象put()实例需要设置room属性,如果UserInRoom和{之间存在关系{1}}(假设存在Room类)。

答案 1 :(得分:0)

为了确保您不是提出异常的人,您可以执行以下操作:

            UserInRoom_entities = []
            # raised? then your .append is missing otherwise something else is wrong
            UserInRoom_entities.append
            UserInRoom_entities.append(user_in_room)