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

时间:2017-06-08 08:33:15

标签: python django python-2.7 django-models django-migrations

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."]

谁能告诉我,谢谢,

3 个答案:

答案 0 :(得分:0)

pub_date

中删除 pub_date = models.DateTimeField(auto_now_add=True)
rm -r Price/migrations #Assume Price is your app_name
rm -rf db.sqlite3

python manage.py makemigrations
python manage.py migrate

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

如果您使用的是SQLite3,请运行

{{1}}

答案 1 :(得分:0)

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

  1. 如果您可以使用所有数据库数据进行刷新,则可以尝试这种方式,
  2. 删除您的数据库文件(如果需要,请进行备份)

    从每个应用中删除迁移文件夹

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

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

    1. 如果您不能使用所有数据库数据进行刷新,可以尝试这种方式,
    2. 使用sqlite console

      删除django_migrations表中的所有条目

      创建新迁移

      迁移他们

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

答案 2 :(得分:0)

删除所有迁移,删除数据库是相当极端的建议,我会建议你反对。事实上,除非您拥有完整的数据备份,并且您的迁移处于版本控制之中,否则不会执行其中任何一种

是的,现在让我们看看DateTimeField

的签名
  

class 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