AttributeError:'decimal.Decimal'对象没有属性'decode'

时间:2018-10-13 04:05:58

标签: python django django-models django-testing

我很难弄清楚为什么会出现此错误。我有一个称为Driver的模型,这是代码:

class Driver(models.Model):
userId = models.IntegerField(unique=True)
status = models.IntegerField(default=-1)
currentLatitude = models.DecimalField(max_digits=11, decimal_places=8, default=0)
currentLongitude = models.DecimalField(max_digits=11, decimal_places=8, default=0)

我正在尝试在tests.py文件中测试该模型,并且在我的setUpTestData(cls)方法中创建了一个像这样的Driver实例。

Driver.objects.create(userId=3, status=0, currentLatitude=37.717, currentLongitude=100)

我只是为经度/纬度放置了伪虚拟值,我想测试此实例的currentLatitude是否实际为37.717,currentLongitude是否为100。

这是我的测试之一:

    def testDriverLatitude(self):
        driver = Driver.objects.get(id=3)
        expected_latitude = driver.currentLatitude
        self.assertEquals(expected_latitude, 37.717)

运行测试时,我在driver = Driver.objects.get(id=1)上遇到错误

这是我得到AttributeError: 'decimal.Decimal' object has no attribute 'decode'

的错误

这是完整的跟踪堆栈

Traceback (most recent call last):
File "\server\backend\tests.py", line 51, in 
testDriverLatitude
driver = Driver.objects.get(id=3)
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\query.py", line 393, in get
num = len(clone)
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\query.py", line 250, in __len__
self._fetch_all()
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\query.py", line 63, in __iter__
for row in compiler.results_iter(results):
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\sql\compiler.py", line 1466, in cursor_iter
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\models\sql\compiler.py", line 1466, in <lambda>
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\django\db\utils.py", line 96, in inner
return func(*args, **kwargs)
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\mysql\connector\cursor_cext.py", line 510, in fetchmany
rows.extend(self._cnx.get_rows(size)[0])
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\mysql\connector\connection_cext.py", line 280, in get_rows
row[i])
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\mysql\connector\conversion.py", line 205, in to_python
return self._cache_field_types[vtype[1]](value, vtype)
File "C:\Users\Andy\.virtualenvs\CS160-wrkGRq_z\lib\site- 
packages\mysql\connector\conversion.py", line 446, in _DECIMAL_to_python
val = value.decode(self.charset)
AttributeError: 'decimal.Decimal' object has no attribute 'decode'

我尝试过在线寻找答案,但似乎找不到解决方法。

任何帮助/提示将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:5)

在使用最新版本的mysql-connector-python时,我遇到了同样的问题。尝试改用8.0.5版。