3

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!

kennycodes
  • 526
  • 3
  • 11
  • 19

1 Answers1

7

I had the same issue when using the newest version of mysql-connector-python. Try using version 8.0.5 instead.

Sonic
  • 71
  • 2
  • I was running into so many filter problems that had to do with `DateTimeField` as well. This solved it! – Stephen Jan 29 '19 at 21:57
  • 4
    Versions as new as 8.0.12 seem to work, failing after 8.0.13 – Jay Feb 09 '19 at 21:19
  • changing from v 8.0.18 to 8.0.05 worked for me too (with Python 3.5 and Django 2.2) – curtisp Nov 10 '19 at 18:56
  • another fix given in the duplicated question is to use `mysql.connector.connect(..., use_pure=True)` or in my case `sa.create_engine('mysql+mysqlconnector://', connect_args=dict(..., use_pure=True))` . See also https://bugs.mysql.com/bug.php?id=90541 – eddygeek May 28 '20 at 19:45
  • I updated it to 8.0.22 – Evgenii Oct 04 '21 at 17:34