gpt4 book ai didi

python - 如何修复 "django.core.exceptions.ValidationError: [“' ' 值的格式无效。它必须是 YYYY-MM-DD HH :MM[:ss[. uuuuuu]][TZ] 格式。”]”

转载 作者:行者123 更新时间:2023-12-05 05:11:04 24 4
gpt4 key购买 nike

#模型.py

class Questions:
question_text = models.CharField(max_length=200)
pub_date = models.DateField(null=True, blank=True, default='')
slug = `models.CharField(max_length=10, unique=True, default="question")`

def __str__(self):
return self.question_text

#views.py

def import_sheet(request):

if request.method == "POST":
form = UploadFileForm(request.POST,
request.FILES)
if form.is_valid():
request.FILES['file'].save_to_database(
name_columns_by_row=2,
model=Question,
mapdict=['question_text', 'pub_date', 'slug'])
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(request, 'perfimport/import.html', {'form': form})

终端输出

Internal Server Error: /import/
Traceback (most recent call last):
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\exception.py", l
ine 34, in inner
response = get_response(request)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\base.py", line 1
15, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\base.py", line 1
13, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Desktop\PerfImport\importer\perfimport\views.py", line 20, in import_sheet
mapdict=['question_text', 'pub_date', 'slug'])
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\django_excel\__init__.py", line
49, in save_to_database
pe.save_as(**params)
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel\core.py", line 82, in s
ave_as
return sources.save_sheet(sheet, **dest_keywords)
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel\internal\core.py", line
48, in save_sheet
return _save_any(a_source, sheet)
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel\internal\core.py", line
60, in _save_any
a_source.write_data(instance)
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel\plugins\sources\db_sour
ces.py", line 65, in write_data
**self._keywords
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel\plugins\renderers\djang
o.py", line 37, in render_sheet_to_stream
**keywords
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel_io\io.py", line 129, in
save_data
**keywords
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel_io\io.py", line 146, in
store_data
writer.write(data)
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel_io\book.py", line 221,
in write
sheet_writer.close()
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pyexcel_io\database\importers\d
jango.py", line 54, in close
self.__objs, batch_size=self.__batch_size
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\manager.py", line 82
, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\query.py", line 474,
in bulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size, ignore_conflicts=ignore_conflicts)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\query.py", line 1211
, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\query.py", line 1186
, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\sql\compiler.py", li
ne 1331, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\sql\compiler.py", li
ne 1275, in as_sql
for obj in self.query.objs
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\sql\compiler.py", li
ne 1275, in <listcomp>
for obj in self.query.objs
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\sql\compiler.py", li
ne 1274, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\sql\compiler.py", li
ne 1215, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\fields\__init__.py",
line 789, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\fields\__init__.py",
line 1273, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\fields\__init__.py",
line 1268, in get_prep_value
return self.to_python(value)
File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\db\models\fields\__init__.py",
line 1243, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'pub_date' value has an invalid date format. It must be in YYYY-MM-DD form

最佳答案

您的默认日期不能为空字符串。因为您允许使用 NULL,所以不要设置默认值,这样如果日期丢失,它将只是 NULL

所以改变:

pub_date = models.DateField(null=True, blank=True, default='')

到:

pub_date = models.DateField(null=True, blank=True)

关于python - 如何修复 "django.core.exceptions.ValidationError: [“' ' 值的格式无效。它必须是 YYYY-MM-DD HH :MM[:ss[. uuuuuu]][TZ] 格式。”]”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55924680/

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