I am having a hard time figuring out why I am getting this error. I have a model called Driver and this is the code:
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)
I am trying to test this model in my tests.py
file and I have a Driver instance created like this in my setUpTestData(cls)
method.
Driver.objects.create(userId=3, status=0, currentLatitude=37.717, currentLongitude=100)
I just put fake dummy values for the lat/long and I want to test if this instance actually has 37.717 for the currentLatitude and 100 for the currentLongitude.
Here are one of my tests:
def testDriverLatitude(self):
driver = Driver.objects.get(id=3)
expected_latitude = driver.currentLatitude
self.assertEquals(expected_latitude, 37.717)
When I run the tests I get an error on the driver = Driver.objects.get(id=1)
and this is the error I get AttributeError: 'decimal.Decimal' object has no attribute 'decode'
Here is the full tracestack
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'
I have tried looking for answers online but can't seem to find a solution.
Any help/tips will be appreciated. Thanks!