开发者

NoneType has no attribute Append

I'm new to Python. I can't understand why a variable is None at a certain point in my code:

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))

The error is:

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'

How is this possible? I'm setting UserInRoom_entities = [] immediately before that call. Or is something else the None in question?

UPDATE: This code works:

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开发者_JAVA百科_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)


Since it appears that my comment to the question had the answer to this, I'll repeat it as an answer, with the hope of gaining some reputation points:

Is the UserInRoom instance initialized properly? I am not familiar with the GAE data model, but I could imagine that the put() ing the instance would require that the room attribute was set, if there is a relationship between UserInRoom and Room (assuming a Room class exists).


To make sure that you're not the one raising exception, you can do something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜