将Django项目升级到Python3-迁移失败

时间:2018-12-14 07:38:17

标签: django python-3.x

我有一个使用Python2.7开发的Django项目,当前使用的是Django版本1.10。我现在正在升级-首先升级到Python3,然后再升级Django。

当我制作Python3虚拟环境并运行测试时:

venv bash% ./manage.py tests

我得到了广泛的追溯:

Traceback (most recent call last):
  File "./manage.py", line 9, in <module>
    execute_from_command_line( sys.argv )
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 72, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/test/runner.py", line 549, in run_tests
    old_config = self.setup_databases()
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/test/runner.py", line 499, in setup_databases
    self.parallel, **kwargs
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/test/runner.py", line 743, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 70, in create_test_db
    run_syncdb=True,
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 130, in call_command
    return command.execute(*args, **defaults)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 202, in handle
    targets, plan, fake=fake, fake_initial=fake_initial
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 97, in migrate
    state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 96, in database_forwards
    schema_editor.create_model(model)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 271, in create_model
    self.quote_name(field.column),
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 115, in quote_name
    return self.connection.ops.quote_name(name)
  File "/home/hove/sleipner/venv/lib/python3.5/site-packages/django/db/backends/sqlite3/operations.py", line 153, in quote_name
    if name.startswith('"') and name.endswith('"'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

据我了解,问题是迁移中的unicode / string问题?我尝试遵循以下建议:“在每个模块的顶部添加from __future__ import unicode_literals”。生成的代码在Python2.7中仍然可以很好地工作,但是manage.py test对于Python3而言以相同的方式失败。

尽管值得,但当我这样做时,该项目似乎可以“基本”地在Python3中工作:

bash% manage.py runserver

更新

这些是我的数据库设置-可能很不对劲,但是请注意它已经在Python2.7中工作了很多年-仍然可以:

if "test" in sys.argv:
    DATABASES = { 'default' : {
        "ENGINE" : "django.db.backends.sqlite3",
        "NAME" : "friskby.sqlite",
        "TEST" : {
            "NAME" : "friskby-test.sqlite"}}}
else:
    DATABASE_URL = os.environ.get("DATABASE_URL")
    if DATABASE_URL:
        config = dj_database_url.config(default = DATABASE_URL)
        DATABASES = { 'default': config }
    else:
        raise Exception("The DATABASE_URL environment variable has not bee set")

1 个答案:

答案 0 :(得分:0)

@Will Keeling的评论使我走上了正确的道路。我的迁移(最初使用Python2.7创建)有很多:

... db_column = b"NAME_OF_COLUMN", ...

声明。我刚刚删除了所有开头的b-瞧瞧,所有测试都在Python2.7和Python3中都通过了。