Datetime.time错误django

时间:2018-02-07 14:03:36

标签: python django

我尝试将数据从excel文件导入Django中的数据库。不幸的是,我在尝试导入时间时遇到此错误。

    AttributeError at /datavisual/upload/
'datetime.time' object has no attribute 'total_seconds'

Models.py

CallRecord(models.Model):
    timeOfCall = models.DurationField()

并在我的Excel逻辑py文件中:

class ExcelParser():

 def read_excel(Document):
    wb2 = load_workbook('Sheet1.xlsx',guess_types=True)
    for row in wb2.active.iter_rows(range_string=wb2.active.calculate_dimension()):
        newRecord = CallRecord.objects.create(timeOfCall = row[1].value)
        newRecord.save()

到目前为止,其他字段都按预期保存,但我不确定导致此问题的原因。

完整错误报告:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/datavisual/upload/

Django Version: 2.0.1
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'datavisual.apps.DatavisualConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args,  **callback_kwargs)

File "C:\Michael\callcentre\callcentre\datavisual\views.py" in model_form_upload
  29.             ExcelParser.read_excel(request.FILES['file'])

File "C:\Michael\callcentre\callcentre\datavisual\ExcelParser.py" in read_excel
  21.               invoiceNumber = row[19].value)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in create
  417.         obj.save(force_insert=True, using=self.db)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in save
  729.                        force_update=force_update, update_fields=update_fields)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in save_base
  759.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in _save_table
  842.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py" in _do_insert
  880.                                using=using, raw=raw)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in _insert
  1125.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1280.             for sql, params in self.as_sql():

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1233.                 for obj in self.query.objs

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1233.                 for obj in self.query.objs

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1232.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in prepare_value
  1172.             value = field.get_db_prep_save(value, connection=self.connection)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
  767.         return self.get_db_prep_value(value, connection=connection, prepared=False)

File "C:\Users\d_aqu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
  1634.         return int(round(value.total_seconds() * 1000000))

Exception Type: AttributeError at /datavisual/upload/
Exception Value: 'datetime.time' object has no attribute 'total_seconds'

1 个答案:

答案 0 :(得分:0)

报告的错误很明显:

AttributeError at /datavisual/upload/
'datetime.time' object has no attribute 'total_seconds'

由于您提供的追溯与您提供的代码不符,因此要确切地告诉您发生了什么并不是那么容易。 无论如何,在某些时候,您可能尝试使用DurationField值初始化datetime.time,而应该填充datetime.timedelta

您可以调试代码并确保Excel文档解析器返回datetime.timedelta个对象而不是datetime.time

看看这一行

newRecord = CallRecord.objects.create(timeOfCall = row[1].value)

并检查row[1].value返回的值,或者如果我信任回溯,请查看文件ExcelParser.py的第21行:

invoiceNumber = row[19].value)

datetime.time值转换为timedelta并不是什么大问题。您只需知道time值的确切含义,并计算相应的持续时间(例如:同一天午夜,同年1月1日等)。有关详细信息,请参阅this post