当我尝试从数据库中获取所有对象时,我在以下代码中收到此错误:
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
字段中的数据存在问题。数据需要采用通用的日期时间格式。
从文档: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'
]