gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:57:12 25 4
gpt4 key购买 nike

我正在使用 django-rest-framework 的简单通用 View ,它在我的本地机器上工作正常,但在服务器中给出了一个属性错误。这是错误:

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

最佳答案

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

我对这个问题的解决方案是在连接到数据库时使用 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,您可以通过向您添加 ?use_pure=True 查询字符串来实现连接 URL。

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

关于Django 属性错误 'datetime.timedelta' 对象没有属性 'decode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com