Django属性错误“ datetime.timedelta”对象没有属性“ decode”

时间:2018-09-23 19:03:10

标签: django python-3.x django-rest-framework

我正在使用django-rest-framework的简单通用视图,该视图在本地计算机上运行良好,但在服务器中出现属性错误。这是错误:

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

这是课程:

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

奇怪的是,所有其他GET和POST API都可以正常工作。这是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'

这是序列化器:

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

我不知道是什么问题。这些版本是:python 3.6.5,Django 2.1,djangorestframework 3.8.2。

错误回溯:

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

3 个答案:

答案 0 :(得分:2)

问题似乎与您使用的数据库连接器有关。

我解决此问题的方法是在连接数据库时使用mysql-connector-c而不是mysql-client

我用 this lib (mysqlclient 1.4.2.post1) 解决了我的问题,但最后还有另一种选择。

步骤是:

  1. pip install mysql-connector-python
  2. pip install mysqlclient
  3. 更改我的设置文件中的数据库设置(在我的情况下为base.py)
    • 来自'default': env.db('DB_DEFAULT', default=f'mysql-connector://root:password@{HOST_MYSQL}:3306/YOUR_DB')
    • 'default': env.db('DB_DEFAULT', default=f'mysql://root:password@{HOST_MYSQL}:3306/YOUR_DB')

另一种方法是将use_pure从数据库选项更改为true,可以通过在连接URL中添加?use_pure=True查询字符串来实现。

这最后一个可能会影响连接性能,因为它确保将使用Python的纯连接器而不是其编译版本进行连接。

答案 1 :(得分:0)

请勿使用:

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

代替使用:

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

答案 2 :(得分:0)

如果使用“ mysql-connector-python”,请尝试使用版本8.0.5。使用最新版本的“ mysql-connector-python”时,我遇到了同样的问题。