2012-05-05 81 views
2

我正在玩Google App Engine,我只是試圖增加一個值(player.number_requests - 見下面的代碼)。但是,當我這樣做時,出現以下錯誤:「TypeError:只能將元組(不是」int「)連接到元組」。任何想法爲什麼發生這種情況?錯誤,當嘗試增量db.IntegerProperty:TypeError:只能連接元組(不是「int」)到元組

class Player(db.Model): 
    name = db.StringProperty(required=True, multiline=False), 
    number_requests = db.IntegerProperty(required=True, default=0), 
    last_seen = db.DateTimeProperty(required=True, auto_now_add=True), 
    created_date = db.DateTimeProperty(required=True, auto_now_add=True) 

class PlayersHandler(webapp2.RequestHandler): 
    def get(self): 
     result = Player.get_by_key_name(self.request.get('name')) 
     if result: 
      result.last_seen = datetime.now() 
      result.number_requests += 1 # FAILS HERE 
      result.put() 
      self.response.out.write('{0}, {1}'.format(result.last_seen, result.number_requests)) 

跟蹤

Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "main.py", line 39, in get 
    result.number_requests += 1 
TypeError: can only concatenate tuple (not "int") to tuple 

回答

9

模型中的屬性有在最後一個逗號(,)。他們不應該。在蟒蛇寫

foo = bar, 

是一樣的,如果你會把

foo = (bar,) 

它創建了一個元組。

+0

哇,我不相信我加了逗號。非常感謝! – Martin

+0

不錯的發現格雷格。 –

相關問題