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

来源:互联网 发布:艾媒咨询知乎 编辑:程序博客网 时间:2024/05/21 17:23
运行环境: 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)



阅读全文
1 0