在Django中迁移数据库

在Django中迁移数据库,django,python-3.x,django-south,Django,Python 3.x,Django South,我正在通过漂亮的视频教程开始学习Django。在视频系列的教程15中,有一个数据库迁移使用south。但是当我执行python manage.py迁移注册时,我遇到了很多错误。第一个错误是: File "C:\Python34\lib\site-packages\south\migration\migrators.py", line 164, i n _run_migration for name, db in south.db.dbs.iteritems(): AttributeE

我正在通过漂亮的视频教程开始学习Django。在视频系列的教程15中,有一个数据库迁移使用south。但是当我执行
python manage.py迁移注册时,我遇到了很多错误。第一个错误是:

  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.0django==1.6.5和python3.4.1

这是my models.py和
的内容。对于您,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`
自动生成的migrations/0002\u auto\u add\u field\u signup\u for\u you\u add\u field\u signup\u 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
为注册运行迁移:
-正向迁移到0002\u自动\u添加\u字段\u注册\u用于\u您\u添加\u字段\u注册\u
更新。
>注册:0002\u自动\u添加\u字段\u注册\u您\u添加\u字段\u注册\u更新
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第175行,i
n\u运行\u迁移
迁移函数()
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第60行,在
返回(λ:方向(orm))
文件“D:\Projects\skillshare\src\signups\migrations\0002\u auto\u add”_
字段\注册\添加\字段\注册\更新.py“,第19行,向前
保持(默认值=False)
文件“C:\Python34\lib\site packages\south\db\sqlite3.py”,第35行,在add\u colu中
锰
field_default=“'%s'”%field.get_db\u prep\u save(默认,连接=self.\u ge
t_连接())
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
350,在get_db_prep_save中
准备就绪(错误)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
911,在get_db_prep_值中
value=self.get\u prep\u值(value)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
895,在get_prep_值中
value=self.to_python(value)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
854,在to_python中
parsed=parse_datetime(值)
文件“C:\Python34\lib\site packages\django\utils\dateparse.py”,第67行,在pa中
rse_日期时间
匹配=日期时间匹配(值)
TypeError:应为字符串或缓冲区
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“C:\Python34\lib\site packages\django\core\management\\uuuu init\uuuuu.py”,第行
399,在从命令行执行命令
utility.execute()
文件“C:\Python34\lib\site packages\django\core\management\\uuuu init\uuuuu.py”,第行
392,执行中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Python34\lib\site packages\django\core\management\base.py”,第242行,
从_argv运行_
self.execute(*args,**选项._dict__;
文件“C:\Python34\lib\site packages\django\core\management\base.py”,第285行,
执行中
输出=self.handle(*args,**选项)
文件“C:\Python34\lib\site packages\south\management\commands\migrate.py”,lin
E111,在手柄上
忽略重影=忽略重影,
文件“C:\Python34\lib\site packages\south\migration\ \uuuu init\uuuuuu.py”,第220行,在
迁移应用程序
success=migrator.migrate\u many(目标、工作计划、数据库)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第256行,i
n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移
结果=migrator.\uuuuu类\uuuuu.migrate\u多(migrator,target,migrations,datab
ase)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第331行,i
n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移n.迁移
结果=self.migrate(迁移,数据库)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第133行,i
n迁移
结果=self.run(迁移、数据库)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第113行,i
跑步
干运行。运行迁移(迁移,数据库)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第192行,i
n运行u迁移
自运行迁移(迁移)
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第178行,i
n\u运行\u迁移
引发异常。FailedDryRun(迁移,sys.exc_info())
south.exceptions.FailedDryRun:!“0002\u自动添加”的试运行期间发现错误_
字段\注册\为您添加\字段\注册\更新'!流产。
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第175行,i
n\u运行\u迁移
迁移函数()
文件“C:\Python34\lib\site packages\south\migration\migrators.py”,第60行,在
返回(λ:方向(orm))
文件“D:\Projects\skillshare\src\signups\migrations\0002\u auto\u add”_
字段\注册\添加\字段\注册\更新.py“,第19行,向前
保持(默认值=False)
文件“C:\Python34\lib\site packages\south\db\sqlite3.py”,第35行,在add\u colu中
锰
field_default=“'%s'”%field.get_db\u prep\u save(默认,连接=self.\u ge
t_连接())
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
350,在get_db_prep_save中
准备就绪(错误)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
911,在get_db_prep_值中
value=self.get\u prep\u值(value)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
895,在get_prep_值中
value=self.to_python(value)
文件“C:\Python34\lib\site packages\django\db\models\fields\\ uuuu init\uuuu.py”,第行
854,在to_python中
parsed=parse_datetime(值)
文件“C:\Python34\lib\site packages\django\utils\dateparse.py”,第67行,在pa中
rse_日期时间
匹配=日期时间匹配(值)
TypeError:应为字符串或缓冲区

有一个问题,您使用布尔值作为
DateTim的默认值(请参阅迁移中第19行的
default=True
import datetime
...
default = datetime.datetime(2016,2,25,16,35,658000)
...