2017-02-05 38 views
0

我有一個模型:Django的遷移錯誤,試圖清理日期字段不良默認

class Season(models.Model): 
    """ Corresponds to your brochure publishing schedule, e.g. Fall/Winter 2012, Sprint 2014, etc. """ 
    name = models.CharField(max_length=45, db_index=True, unique=True) 
    start_date = models.DateField(db_index=True, help_text="The date enrollment can begin this Season.") 

    <snip> 

模型預日期遷移和我沒有用南方。

的起始日期字段中生活了很多年,這個定義

start_date = models.DateField(db_index=True, default=False, 
            help_text="The date of the first Session of this Season.") 

其實我去到編輯說明文字,並注意到默認=假和思想,是沒有道理的日期字段,從我愚蠢的時候,它一定是一個殘餘物。

所以我把它拿出來了。

現在我的遷移:

class Migration(migrations.Migration): 

    dependencies = [ 
     ('district', '0012_auto_20160622_1741'), 
    ] 

    operations = [ 
     migrations.AlterField(
      model_name='season', 
      name='start_date', 
      field=models.DateField(help_text=b'The date enrollment can begin this Season.', db_index=True), 
     ), 
    ] 

與失敗:

Applying district.0013_auto_20170204_1811...Traceback (most recent call last): 
    File "manage.py", line 9, in <module> 
    execute_from_command_line(sys.argv) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line 
    utility.execute() 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute 
    output = self.handle(*args, **options) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate 
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards 
    schema_editor.alter_field(from_model, from_field, to_field) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field 
    old_db_params, new_db_params, strict) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 566, in _alter_field 
    old_default = self.effective_default(old_field) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default 
    default = field.get_db_prep_save(default, self.connection) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save 
    prepared=False) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value 
    return self.to_python(value) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1274, in to_python 
    parsed = parse_date(value) 
    File "/verylongpathtovenv/lib/python2.7/site-packages/django/utils/dateparse.py", line 60, in parse_date 
    match = date_re.match(value) 
TypeError: expected string or buffer 

研究類型錯誤:預期的字符串或緩衝區一個有關沒有做什麼,我想討論上來撤消,即不要將日期字段的默認設置爲不像日期的東西。

不清楚如何繼續此操作。任何提示將不勝感激。

+0

是由makemigrations命令創建的遷移還是您自己做的?爲什麼help_text是一個字節串? – trixn

+0

它是用makemigrations創建的。我不確定爲什麼django將幫助文本看作字節串。 –

回答

1
  1. 確定默認是在數據庫中的哪些

    cd /path/to/project 
    python manage.py dbshell 
    mysql> show create table district_season\G 
    # or for postgres 
    # pg_dump -t 'schema.district_season' --schema-only database-name 
    
  2. 如果沒有與該類型沒問題,用--fake

    ./manage.py migrate district --fake 
    

    運行遷移,這將標誌着遷移是在沒有對數據庫做任何事情的情況下運行的。