Django属性错误"datetime.timedelta"对象没有属性"decode" [英] Django Attribute error 'datetime.timedelta' object has no attribute 'decode'

查看:64
本文介绍了Django属性错误"datetime.timedelta"对象没有属性"decode"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

这是课程:

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

奇怪的是,所有其他GET和POST API都可以正常工作.这是TblCart:

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'

这是序列化器:

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

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

I cannot figure out what the problem is. The versions are: 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

推荐答案

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

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

我对这个问题的解决方案是在连接到数据库时使用 mysql-connector-c 而不是 mysql-client .

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

我使用这个库(mysqlclient 1.4.2.post1)解决了我的问题,但是最后是另一种选择.

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

步骤是:

  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 ,您可以通过向您添加?use_pure = True 查询字符串来实现连接网址.

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.

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

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.

这篇关于Django属性错误"datetime.timedelta"对象没有属性"decode"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆