2012-10-26 121 views
3

所以,我正在學習使用South,並且我在幾個問題中運行。我做了模型中的初始應用程序遷移看起來像這樣:Django-South遷移期間的ValidationError

class Poll(models.Model): 
    pass 

然後加入我的領域......

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

,並在遷移過程中南方抱怨說,這些字段沒有默認值,所以我輸入了一些,但它沒有工作......現在,當我嘗試從模型中刪除它們和遷移回空的模式,我得到以下回溯:

[email protected]:~/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."] 

我怎麼能解決這個問題,和世界衛生大會t是添加這些字段+從空模型遷移到字段填充模型的正確方法?我一直在通過南的文檔,但我不明白...我做了所有的事情according to the documentation,但我不知道我應該默認DateTimeField到...

+0

您可以加入到這個問題0003_auto__add_field_poll_question__add_field_poll_pub_date.py的內容(這是不是太大)? –

回答

1

當時你必須提供一個默認值,你正在與一個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) 
+0

我已經這樣做了...仍然會有這個醜陋的回溯:http://pastebin.com/qvLgamB3 –

+0

你確定嗎?最後一行顯示「ValidationError:[u''0'值的格式無效,必須採用YYYY-MM-DD HH:MM [:ss [.uuuuuu]] [TZ]格式。當它提到「0」時,它似乎沒有正確傳遞。否則,嘗試將時間指定爲一個字符串,即「2012-10-01 17:30」。 – jro