0

キーを使用してエンティティを更新したいのですが、何か問題があります。ここに私のコード:

def post(self):
        username = self.request.get("contentb")
        header, response = client.request('https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name='+username+'&count=5000')
        ids = [str(id) for id in json.loads(response)["ids"]]
        messages = []
        userlist = [user.username for user in User.query().fetch(projection=["username"])]
        if username not in userlist:
            user = User()
            user.username = username
            user.follower_list = ids
            key = user.put()
            messages.append("Ok we added you")
        else:
            followerlist = [user.follower_list[0] for user in User.query().filter(User.username == username).fetch(projection=["follower_list"])]
            if len(set(followerlist)-set(ids)) == 0:
                messages.append("No one has unfollowed you :)")
            else:
                for i in set(followerlist)-set(ids):
                    messages.append(str(i) + " " + "unfollowed you")  

            user = key.get()
            user.follower_list = ids
            user.put()
            messages.append("We updated your followerlist")

しかし、私はエラーが発生します:

user = key.get()
UnboundLocalError: local variable 'key' referenced before assignment

どこが間違っているか教えてください。

4

2 に答える 2

0
def post(self):
        username = self.request.get("contentb")
        header, response = client.request('https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name='+username+'&count=5000')
        ids = [str(id) for id in json.loads(response)["ids"]]
        messages = []
        userlist = [user.username for user in User.query().fetch(projection=["username"])]
        if username not in userlist:
            user = User()
            user.username = username
            user.follower_list = ids
            key = user.put()
            messages.append("Ok we added you")

        #now user should be in; lets fetch it (update to your needs)
        user = [user for user in User.query().fetch(projection=["username"]) if user.username == username][0]      
        followerlist = [user.follower_list[0] for user in User.query().filter(User.username == username).fetch(projection=["follower_list"])]
        if len(set(followerlist)-set(ids)) == 0:
            messages.append("No one has unfollowed you :)")
        else:
            for i in set(followerlist)-set(ids):
                messages.append(str(i) + " " + "unfollowed you")  

        user.follower_list = ids
        user.put()
        messages.append("We updated your followerlist")
于 2013-09-07T10:58:02.970 に答える