gpt4 book ai didi

python - 属性错误 : 'decimal.Decimal' object has no attribute 'decode'

转载 作者:行者123 更新时间:2023-11-28 20:32:24 24 4
gpt4 key购买 nike

<分区>

我很难弄清楚为什么会出现此错误。我有一个名为 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.objects.create(userId=3, status=0, currentLatitude=37.717, currentLongitude=100)

我只是为纬度/经度设置了假的虚拟值,我想测试这个实例是否真的有 37.717 的 currentLatitude 和 100 的 currentLongitude。

这是我的一个测试:

    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'

我尝试在网上寻找答案,但似乎找不到解决方案。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com