django.core.exceptions.ValidationError: [u"'' 值无效

Posted

技术标签:

【中文标题】django.core.exceptions.ValidationError: [u"\'\' 值无效【英文标题】:django.core.exceptions.ValidationError: [u"'' value has an invaliddjango.core.exceptions.ValidationError: [u"'' 值无效 【发布时间】:2017-11-09 20:44:44 【问题描述】:

models.py

    # -*- coding:utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models
    # Create your models here.

    class moption(models.Model):

        pub_date = models.DateTimeField('pub time', auto_now_add=True, editable=True)

当我命令“python manage.py migrate”时

Operations to perform:
  Apply all migrations: Price, admin, auth, contenttypes, sessions
Running migrations:
  Applying Price.0006_moption_pub_date...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\schema.py", line 43, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 395, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 147, in column_sql
    default_value = self.effective_default(field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1440, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1419, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1403, in to_python
    params='value': value,
django.core.exceptions.ValidationError: [u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

谁能告诉我,谢谢,

【问题讨论】:

您的代码似乎正确。你能在这里发布你的迁移文件吗?可能是迁移代码有问题 【参考方案1】:

您的迁移文件似乎与数据库中的表结构不匹配。

    如果您可以刷新所有数据库数据,您可以尝试这种方式,

删除您的数据库文件(如果需要,请备份)

从每个应用程序中删除您的迁移文件夹

创建新的迁移,迁移它们。

现在,您有了一个带有新表结构的新 db 文件。

    如果您不满意刷新所有数据库数据,您可以尝试这种方式,

使用 sqlite 控制台从 django_migrations 表中删除所有条目

创建新的迁移

迁移他们

现在,您有了一个带有新表结构的新 db 文件。

【讨论】:

【参考方案2】:

删除所有迁移、删除数据库是相当极端的建议,我建议您不要这样做。事实上,不要这样做,除非您有完整的数据备份并且您的所有迁移都在版本控制中。

好的,现在我们来看看DateTimeField的签名

类 DateTimeField(auto_now=False, auto_now_add=False, **options)

还有你的使用情况

  pub_date = models.DateTimeField('pub time', auto_now_add=True, editable=True)

这个“酒吧时间”是什么时候?不管它是什么,它都会导致创建错误的迁移。

修复代码后,查看您的数据库,查看django_migrations 表内部,并找到该应用最后一次应用迁移的编号。迁移文件夹中编号较大的任何内容都是未应用的迁移。仅删除未应用的迁移。然后删除 dame 文件夹中的 '*.pyc' 文件。现在做

./manage.py makemigrations
./manage.py migrate

【讨论】:

【参考方案3】:

pub_date 中删除editable=True

    pub_date = models.DateTimeField(auto_now_add=True)

尝试清除所有迁移并重新创建数据库,

如果你使用的是 SQLite3,那么运行,

rm -r Price/migrations #Assume Price is your app_name
rm -rf db.sqlite3

python manage.py makemigrations
python manage.py migrate

【讨论】:

你删除它并运行python manage.py makemigrations Price然后,python manage.py migrate 当然,当我运行 python manager.py makemigrations,打印没有检测到更改,然后 python manage.py migrate,打印同样的错误 您需要指定应用名称,有时无法检测到更改。所以运行python manage.py makemigrations Price 或任何您的应用名称... 现在我删除了pub_date这个文件,同样的错误,为什么这么神奇? 你是在使用 sqlite3 作为数据库吗

以上是关于django.core.exceptions.ValidationError: [u"'' 值无效的主要内容,如果未能解决你的问题,请参考以下文章