django - Django-South 迁移期间的 ValidationError

标签 django django-models django-south

所以,我正在学习使用 South,并且我已经处理了一些问题。我使用如下模型进行了初始应用程序迁移:

class Poll(models.Model):
    pass

然后添加我的字段...

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField()

在迁移期间,South 提示这些字段没有默认值,所以我输入了一些但没有用...现在当我尝试从模型中删除它们并迁移回空模式时,我得到以下回溯:

mirkocrocop@Mirkos-MacBook-Pro:~/workspace/toastdriven$ python manage.py migrate polls
Running migrations for polls:
 - Migrating forwards to 0007_auto__del_field_poll_question.
 > polls:0003_auto__add_field_poll_question__add_field_poll_pub_date
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle
    ignore_ghosts = ignore_ghosts,
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/__init__.py", line 213, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 235, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 310, in migrate_many
    result = self.migrate(migration, database)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 106, in run
    dry_run.run_migration(migration)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 191, in run_migration
    self._run_migration(migration)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 178, in _run_migration
    raise exceptions.FailedDryRun(migration, sys.exc_info())
south.exceptions.FailedDryRun:  ! Error found during dry run of '0003_auto__add_field_poll_question__add_field_poll_pub_date'! Aborting.
Traceback (most recent call last):
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 175, in _run_migration
    migration_function()
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda>
    return (lambda: direction(orm))
  File "/Users/mirkocrocop/workspace/toastdriven/polls/migrations/0003_auto__add_field_poll_question__add_field_poll_pub_date.py", line 19, in forwards
    keep_default=False)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/db/sqlite3.py", line 31, in add_column
    field.column: self._column_sql_for_create(table_name, name, field, False),
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/db/sqlite3.py", line 189, in _column_sql_for_create
    sql = self.column_sql(table_name, name, field, with_name=False, field_prepared=True)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/south/db/generic.py", line 688, in column_sql
    default = field.get_db_prep_save(default, connection=self._get_connection())
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 292, in get_db_prep_save
    prepared=False)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 816, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 801, in get_prep_value
    value = self.to_python(value)
  File "/Users/mirkocrocop/.virtualenvs/toastdriven/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 785, in to_python
    raise exceptions.ValidationError(msg)
ValidationError: [u"'0' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

我该如何解决这个问题,添加这些字段+从空模型迁移到字段填充模型的正确方法是什么?我一直在浏览 South 的文档,但我不明白......我做了一切 according to the documentation ,但我不确定应该将 DateTimeField 默认设置为什么...

最佳答案

当您必须提供默认值时,您正在与 Python 解释器进行交互。如前所述,datetime 模块在那个时间点可用。在这种情况下,要为 DateTimeField 提供默认值,只需提供一个 datetime 对象:

# Use the current date and time
datetime.datetime.now()
# Or a specific date and time
datetime.datetime(2012, 10, 1, 17, 30, 0, 0)

关于django - Django-South 迁移期间的 ValidationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13088512/

相关文章:

Django:model.py 中添加的新类未显示在管理站点中

django - 运行单元测试时禁用 Django South?

django - 比较 Django 模板中的 url

javascript - Django:无法使用 Javascript 访问 sessionid cookie

django - 仅在 IE9 上使用 django 表单提交的 HTTP 403

django - django 中管理模型类的详细名称

python - 无法使用 MEDIA_ROOT 和 MEDIA_URL 在 Django 中上传图片

Django South 和约束 ID

django - 从南迁移历史中删除迁移

python - Django 数字过滤器模板