django - 将 Django 项目升级到 Python3 - 迁移失败
问题描述
我有一个用 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")
解决方案
@Will Keeling 的评论让我走上了正轨。我的迁移(最初使用 Python2.7 创建)有很多:
... db_column = b"NAME_OF_COLUMN", ...
陈述。我刚刚删除了所有前导b
- 瞧,所有测试在 Python2.7 和 Python3 中都通过了。
推荐阅读
- html - 使绝对定位的孩子随他们的上级动态调整大小
- python - 在 python/selenium 中解析加载站点
- python - 我需要更改数据值的名称
- sql - 在 sql Query.neo 中需要帮助
- javascript - TypeError:“x”不可迭代
- javascript - 将 .rotatable 添加到对象将其初始度数返回为 0
- tableau-api - 如何比较同一列的两个平均值?
- c++ - qpushbutton 奇怪的自动点击
- sql - 如何通过一个变量进入 SQL oracle nth_value 顺序,windowing_clause 取决于另一个变量?
- javascript - 如何在 gulp/node 中预渲染 React 应用程序?