在Django中迁移数据库

时间:2014-08-20 11:05:13

标签: django python-3.x django-south

我通过this精彩的视频教程开始使用Django。在视频系列的教程15中,使用进行数据库迁移。但是当我做python manage.py migrate signups时,我遇到了很多错误。第一个错误是:

  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 164, i
n _run_migration
    for name, db in south.db.dbs.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

我将iteritems()更改为items()以解决此问题,但是还会出现很多其他错误。我的猜测是它与行动中的版本有关 - South==1.0 Django == 1.6.5 and Python 3.4.1

以下是我的 models.py 的内容,for_you, timestamp, updated是迁移后添加的属性。最初注释掉了这些属性。

 `from django.db import models


class SignUp(models.Model):
  for_you = models.BooleanField(default = True) 
  first_name = models.CharField(max_length = 120, null=True, blank=True)
  last_name = models.CharField(max_length = 120, null=True, blank=True)
  email = models.EmailField()        
  timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
  updated = models.DateTimeField(auto_now_add = False, auto_now = True, default=True)
  #timestamp = models.DateTimeField(auto_now_add = False, auto_now = True)
  #timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)

 def __str__(self):
     return self.email`

自动生成的迁移/ 0002_auto__add_field_signup_for_you__add_field_signup_updated.py 看起来像

# -*- coding: utf-8 -*-

from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

def forwards(self, orm):
    # Adding field 'SignUp.for_you'
    db.add_column('signups_signup', 'for_you',
                  self.gf('django.db.models.fields.BooleanField')(default=True),
                  keep_default=False)

    # Adding field 'SignUp.updated'
    db.add_column('signups_signup', 'updated',
                  self.gf('django.db.models.fields.DateTimeField')(blank=True, default=True, auto_now=True),
                  keep_default=False)


def backwards(self, orm):
    # Deleting field 'SignUp.for_you'
    db.delete_column('signups_signup', 'for_you')

    # Deleting field 'SignUp.updated'
    db.delete_column('signups_signup', 'updated')


models = {
    'signups.signup': {
        'Meta': {'object_name': 'SignUp'},
        'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
        'first_name': ('django.db.models.fields.CharField', [], {'blank': 'True', 'null': 'True', 'max_length': '120'}),
        'for_you': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
        'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
        'last_name': ('django.db.models.fields.CharField', [], {'blank': 'True', 'null': 'True', 'max_length': '120'}),
        'timestamp': ('django.db.models.fields.DateTimeField', [], {'blank': 'True', 'auto_now_add': 'True'}),
        'updated': ('django.db.models.fields.DateTimeField', [], {'blank': 'True', 'default': 'True', 'auto_now': 'True'})
    }
}

complete_apps = ['signups']

这是完整的错误日志:

Running migrations for signups:
 - Migrating forwards to 0002_auto__add_field_signup_for_you__add_field_signup_u
pdated.
 > signups:0002_auto__add_field_signup_for_you__add_field_signup_updated
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 175, i
n _run_migration
    migration_function()
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 60, in
 <lambda>
    return (lambda: direction(orm))
  File "D:\Projects\skillshare\src\signups\migrations\0002_auto__add_
field_signup_for_you__add_field_signup_updated.py", line 19, in forwards
    keep_default=False)
  File "C:\Python34\lib\site-packages\south\db\sqlite3.py", line 35, in add_colu
mn
    field_default = "'%s'" % field.get_db_prep_save(default, connection=self._ge
t_connection())
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 350, in get_db_prep_save
    prepared=False)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 911, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 895, in get_prep_value
    value = self.to_python(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 854, in to_python
    parsed = parse_datetime(value)
  File "C:\Python34\lib\site-packages\django\utils\dateparse.py", line 67, in pa
rse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
399, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 242,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 285,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\south\management\commands\migrate.py", lin
e 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "C:\Python34\lib\site-packages\south\migration\__init__.py", line 220, in
 migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 256, i
n migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, datab
ase)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 331, i
n migrate_many
    result = self.migrate(migration, database)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 133, i
n migrate
    result = self.run(migration, database)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 113, i
n run
    dry_run.run_migration(migration, database)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 192, i
n run_migration
    self._run_migration(migration)
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 178, i
n _run_migration
    raise exceptions.FailedDryRun(migration, sys.exc_info())
south.exceptions.FailedDryRun:  ! Error found during dry run of '0002_auto__add_
field_signup_for_you__add_field_signup_updated'! Aborting.
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 175, i
n _run_migration
    migration_function()
  File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 60, in
 <lambda>
    return (lambda: direction(orm))
  File "D:\Projects\skillshare\src\signups\migrations\0002_auto__add_
field_signup_for_you__add_field_signup_updated.py", line 19, in forwards
    keep_default=False)
  File "C:\Python34\lib\site-packages\south\db\sqlite3.py", line 35, in add_colu
mn
    field_default = "'%s'" % field.get_db_prep_save(default, connection=self._ge
t_connection())
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 350, in get_db_prep_save
    prepared=False)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 911, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 895, in get_prep_value
    value = self.to_python(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 854, in to_python
    parsed = parse_datetime(value)
  File "C:\Python34\lib\site-packages\django\utils\dateparse.py", line 67, in pa
rse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

2 个答案:

答案 0 :(得分:1)

存在这个问题,您使用布尔值作为default=True列的默认值(请参阅迁移中第19行的DateTime)。那不行。只需从模型中删除default=True并重新生成迁移。

您可能需要该列中的null=True或基于时间的默认值。

答案 1 :(得分:0)

在迁移过程中,fields.DateTimeField不能是布尔值(默认值= True)。 您可以编辑迁移设置日期时间值

import datetime
...
default = datetime.datetime(2016,2,25,16,35,658000)
...

models.DateTimeField应该是Nonedatetime对象