3

I am using a simple generic view of django-rest-framework that is working fine on my local machine but gives an attribute error in the server. This is the error:

AttributeError at /api/getcarts/ 'datetime.timedelta' object has no attribute 'decode'

Here is the class:

class GetCarts(generics.ListAPIView):
     serializer_class = CartSerializer
     queryset = TblCarts.objects.all()

The strange thing is, all the other GET and POST APIs are working fine. Here is the TblCart:

class TblCarts(models.Model):
     price = models.IntegerField()
     location = models.CharField(max_length=500)
     location_coordinate = models.CharField(max_length=100, default=0)
     number = models.CharField(max_length=50)
     promo_code = models.CharField(max_length=50, default=0)
     receipt = models.CharField(max_length=100)
     order_receive_date = models.DateField(auto_now_add=True)
     order_receive_time = models.TimeField(auto_now_add=True)
     order_dispatch_time = models.TimeField(default='00:00', max_length=100)
     order_delivered_time = models.TimeField(default='00:00', max_length=100)
     order_status = models.CharField(max_length=100, default=1)

    class Meta:
         managed = False
         db_table = 'tbl_carts'

Here is the serializer:

class CartSerializer(serializers.ModelSerializer):
    class Meta:
         model = TblCarts
         fields = '__all__'

I cannot figure out what the problem is. The versions are: python 3.6.5, Django 2.1, djangorestframework 3.8.2.

The error traceback:

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  483.             response = self.handle_exception(exc)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
  443.             self.raise_uncaught_exception(exc)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
  480.             response = handler(request, *args, **kwargs)

File "/var/www/khaanpin/khanpinuser/api/views.py" in get
  51.         return Response({'error': 'false', 'data': CartSerializer(orders, many=True).data})

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/serializers.py" in data
  765.         ret = super(ListSerializer, self).data

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/serializers.py" in data
  262.                 self._data = self.to_representation(self.instance)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
  683.             self.child.to_representation(item) for item in iterable

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
  268.         self._fetch_all()

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
  1183.             self._result_cache = list(self._iterable_class(self))

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
  63.         for row in compiler.results_iter(results):

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in cursor_iter
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in <lambda>
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/django/db/utils.py" in inner
  96.                 return func(*args, **kwargs)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/mysql/connector/cursor_cext.py" in fetchmany
  510.             rows.extend(self._cnx.get_rows(size)[0])

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/mysql/connector/connection_cext.py" in get_rows
  280.                                                               row[i])

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/mysql/connector/conversion.py" in to_python
  205.             return self._cache_field_types[vtype[1]](value, vtype)

File "/var/www/khaanpin/khanpinuser/venv3/lib/python3.6/site-packages/mysql/connector/django/base.py" in _TIME_to_python
  106.         return dateparse.parse_time(value.decode('utf-8'))

Exception Type: AttributeError at /api/getcarts/
Exception Value: 'datetime.timedelta' object has no attribute 'decode'
Request information:
USER: 9999999999

GET: No GET data

POST: No POST data

FILES: No FILES data

COOKIES: No cookie data
1
  • That is not the view code that you are using. As the traceback shows, you have a view that defines a get method that contains the code return Response({...}). You should show that view. Commented Sep 23, 2018 at 19:15

3 Answers 3

3

The problems seems to be related with the database connector you're using.

My solution to this problem was using mysql-connector-c instead of mysql-client when connecting to the database.

I used this lib (mysqlclient 1.4.2.post1) to solve my issue but there is another option at the end.

Steps were:

  1. pip install mysql-connector-python
  2. pip install mysqlclient
  3. change the database settings in my settings file (base.py in my case)
    • from 'default': env.db('DB_DEFAULT', default=f'mysql-connector://root:password@{HOST_MYSQL}:3306/YOUR_DB')
    • to 'default': env.db('DB_DEFAULT', default=f'mysql://root:password@{HOST_MYSQL}:3306/YOUR_DB')

Another option is changing the use_pure from db options to true, you can do it by adding the ?use_pure=True querystring to you connection URL.

This last one could affect the connection performance because it assures that it will be made with Python's pure connector and not the compile version of it.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

don't use:

 order_dispatch_time = models.TimeField(default='00:00', max_length=100)
 order_delivered_time = models.TimeField(default='00:00', max_length=100)

instead use :

 import datetime
  <...you model ...>
 order_dispatch_time = models.TimeField(default=datetime.time(00,00))
 order_delivered_time = models.TimeField(default=datetime.time(00,00))

1 Comment

unfortunately still the same exact error. There is some issue related to the datetime itself, when I comment all the datetime related fields in the model it works fine, when even a single datetime field is there with or without a default value, the same error pops up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.