建立下zzuliacgn的novel模块时,碰到了一些错误。因为确实没试过小说网站数据库设计,顺便用Django的model做一些新的尝试。
运行环境 Runtime environment
1 | 操作系统 : Windows10 |
症状
Django查询models对象报错:TypeError: str returned non-string (typedecimal.Decimal)
错误代码如下:1
2
3
4
5
6
7
8
9
10
11class 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
发现时由于函数返回的是str,但是之前传的值是Decimal所导致。
解决办法
在这里修改一下返回值把如下部分的代码1
2def __str__(self):
return self.Close
将其修改为:1
2def __str__(self):
return str(self.Close)
即可解决这个问题!类似的错误还有TypeError: str returned non-string (type tuple)。同理!