簡體   English   中英

如何修復“django.core.exceptions.ValidationError: [“'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.”]”

[英]How to fix "django.core.exceptions.ValidationError: [“'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.”]"

#models.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})

端子 output

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

這是一個錯誤,來自您嘗試放入數據庫的數據,或者來自您擁有的表單。 它似乎更有可能來自模型驗證,但我不完全確定沒有表單類。

我假設這是來自問題模型上的pub_date字段,因此輸入的數據不是表單/模型所期望的格式。 如果您可以共享實際數據(或一個有用的示例),我們可以幫助您排除導致錯誤的確切原因。

您的默認日期不能是空字符串。 由於您允許NULL ,因此不要設置默認值,如果缺少日期,那么它將只是NULL

所以改變:

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

至:

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

如果您正在運行此代碼來創建表,那么它將給出一個驗證錯誤,因為Date必須是DATE 格式而不是Default="" 索恩。 1 => 盡量不要提及default='' Soln.2 => 如果您正在更改表,如果您收到上述錯誤,那么您必須刪除所有遷移文件夾並再次運行 models.py。 這個對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM