2014-09-21 52 views
0

我有一個模型:保存浮到FloatField的Django模型,爲什麼強迫爲Unicode:需要字符串或緩衝區,浮動發現

class Conditions(models.Model): 
     date_time = models.DateTimeField(blank=True, null=True) 
     temperature = models.FloatField(blank=True, null=True) 
     humidity = models.FloatField(blank=True, null=True) 

     def __unicode__(self): 
      return self.temperature 

我有數據加載到從谷歌docs.I進口模型「米節能是像這樣:

conditions_obj = Conditions.objects.get_or_create(
        date_time=datetime.datetime.strptime(row[0],'%m/%d/%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S'), 
        temperature=float(row[1]), 
        humidity=float(row[2])) 

當我這樣做,我得到這個錯誤,我不知道爲什麼:

TypeError: coercing to Unicode: need string or buffer, float found 

做我的T溫度值需要保存爲一個字符串,因爲如果我這樣做,那麼事情就會起作用。只是似乎集市。

回答

0

問題沒有保存:問題是您的__unicode__方法。正如名稱明確暗示的那樣,它需要返回一個unicode值,而不是float。

你可以明確地將其轉換:

def __unicode__(self): 
    return unicode(self.temperature) 
+0

我猜我以爲__unicode __(個體經營)將返回unicode字符串。非常感謝! – brownshoes 2014-09-22 13:46:01

相關問題