Django查询models对象报错:TypeError: __str__ returned non-string (type decimal.Decimal)

运行环境: python 3.6.1 Django 1.11.3
 modles.py 添加了如下的对象:
class ApplStockPrice(models.Model):
    Date = models.CharField(max_length=20)
    Open = models.DecimalField(max_digits=20,decimal_places=5)
    High = models.DecimalField(max_digits=20,decimal_places=5)
    Low = models.DecimalField(max_digits=20,decimal_places=5)
    Close = models.DecimalField(max_digits=20,decimal_places=5)
    Adj_Close = models.DecimalField(max_digits=20,decimal_places=6)
    Volume = models.DecimalField(max_digits=20,decimal_places=2)

    def __str__(self):
        return self.Close


mysql数据库中的数据:



错误原因解析:


函数返回的是str,但是之前传的值是Decimal。


解决方式一:


手工修改 __str__() 函数;


解决方式二:



类似的错误:


TypeError: __str__ returned non-string (type tuple)



猜你喜欢

转载自blog.csdn.net/wjunsing/article/details/78104662