python - Django 属性错误 : 'str' object has no attribute 'tzinfo'

标签 python mysql django tzinfo

当我尝试从我的数据库中获取所有对象时,我在以下代码中收到此错误:

data1 = Data.objects.all()
for dataset in data1:

这是我的模型:
class Data(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
path = models.TextField(db_column='Path')  # Field name made lowercase.
username = models.ForeignKey('Users', models.DO_NOTHING, db_column='Username')  # Field name made lowercase.
datatype = models.CharField(db_column='Datatype', max_length=20, blank=True, null=True)  # Field name made lowercase.
filesize = models.FloatField(db_column='Filesize', blank=True, null=True)  # Field name made lowercase.
creationdate = models.DateTimeField(db_column='CreationDate')  # Field name made lowercase.
modificationdate = models.DateTimeField(db_column='ModificationDate')  # Field name made lowercase.
diskname = models.CharField(db_column='Diskname', max_length=100, blank=True, null=True)  # Field name made lowercase.

class Meta:
    managed = False
    db_table = 'Data'

完整的错误信息是:
    Internal Server Error: /files/
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/pi/.local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/django/views/decorators/cache.py", line 31, in _cache_controlled
    response = viewfunc(request, *args, **kw)
  File "/home/pi/.local/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/pi/MakMakula/FileManager/app/views.py", line 57, in index
    for dataset in data1:
  File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
    self._fetch_all()
  File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/query.py", line 74, in __iter__
    for row in compiler.results_iter(results):
  File "/home/pi/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1095, in apply_converters
    value = converter(value, expression, connection)
  File "/home/pi/.local/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 265, in convert_datetimefield_value
    value = timezone.make_aware(value, self.connection.timezone)
  File "/home/pi/.local/lib/python3.7/site-packages/django/utils/timezone.py", line 270, in make_aware
    return timezone.localize(value, is_dst=is_dst)
  File "/home/pi/.local/lib/python3.7/site-packages/pytz/__init__.py", line 237, in localize
    if dt.tzinfo is not None:
AttributeError: 'str' object has no attribute 'tzinfo'
[02/Jun/2020 10:43:44] "GET /files/ HTTP/1.1" 500 110853

有没有人知道为什么会出现这种情况?

最佳答案

您来自 DateTimeField 的数据存在问题 field 。数据需要采用通用的 DateTime 格式。

来自文档:https://docs.djangoproject.com/en/3.0/ref/forms/fields/#datetimefield

Normalizes to: A Python datetime.datetime object.

Validates that the given value is either a datetime.datetime, \
datetime.date or string formatted in a particular datetime format.

您的数据必须符合以下条件之一:
[
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
]

关于python - Django 属性错误 : 'str' object has no attribute 'tzinfo' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62150353/

相关文章:

python - pvmismatch 的语法。如何更改特定单元格?

python - PyInstaller 与 Pymongo 的问题

python - Pyspark - 调用 pandas_udf 时出错,返回 Series.interpolate() 作为结果

PHP将mysql数据导出到excel表但在浏览器中显示

python - 使用可重用方法和错误处理设置基于类的 View 架构

python - Django 表单在生产与 LH 上找不到正确的 ID int 增量

python - 获取列表中两个字符串之间的位置差异python

php - 向 Mysql 数据库发送两个查询时出现问题

mysql高级组查询

Django Rest Framework 将数据从序列化器保存到模型