升级到 1.8 后 makemigrations 时出现 DjangoUnicodeDecodeError
DjangoUnicodeDecodeError while makemigrations after upgrading to 1.8
最近我将 Django 从 1.6.5 升级到了 1.8.3。我一直在使用 South,因此,在 tutorial 之后,我删除了 South,删除了旧的迁移和 运行 makemigrations。然后我得到错误:
[mefioo@ibmed-server kariera_naukowa]$ env/bin/python src/manage.py makemigrations
Migrations for 'editcv':
0001_initial.py:
- Create model Adresy
- Create model Affiliacja
[...all models from models.py...]
- Create model ZalRo
- Add field WszysCz to wlasne
[...so far only ForeignKeys, but not all of them...]
- Add field user to adresy
Traceback (most recent call last):
File "src/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 75, in _write
arg_string, arg_imports = MigrationWriter.serialize(item)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 296, in serialize
value = force_text(value)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/utils/encoding.py", line 102, in force_text
raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x268a9d0> (<class 'django.utils.functional.__proxy__'>)
我查了一下,0xc3 是 Ã 或 Ä 我当然不会使用(它甚至不存在于波兰字母中)。
通过的fk之一是
wydzial = models.ForeignKey(Wydzial, verbose_name="Wydział")
它包含波兰语 'ł',但一切正常。
我在这里错过了什么?
如果您在模型中使用 unicode 字符串,则应避免出现问题,例如verbose_name=u"Wydział"
。
您可能更愿意从未来的库中导入 unicode_literals,如 recommended by the Django docs。不过,如果将其添加到现有模块中,请小心!
from __future__ import unicode_literals
Unicode 与 python 字符串问题。 Django 文档已经朝着 python 3 的方向进一步发展,因此他们不再习惯于警告您了。在python2中,在字符串前面加上一个u,例如u"Wydział"
。由于您是 运行 2.7,您可以尝试 from __future__ import unicode_literals
。请注意,第二个解决方案可能会破坏您项目中的其他一些东西。
最近我将 Django 从 1.6.5 升级到了 1.8.3。我一直在使用 South,因此,在 tutorial 之后,我删除了 South,删除了旧的迁移和 运行 makemigrations。然后我得到错误:
[mefioo@ibmed-server kariera_naukowa]$ env/bin/python src/manage.py makemigrations
Migrations for 'editcv':
0001_initial.py:
- Create model Adresy
- Create model Affiliacja
[...all models from models.py...]
- Create model ZalRo
- Add field WszysCz to wlasne
[...so far only ForeignKeys, but not all of them...]
- Add field user to adresy
Traceback (most recent call last):
File "src/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 75, in _write
arg_string, arg_imports = MigrationWriter.serialize(item)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/db/migrations/writer.py", line 296, in serialize
value = force_text(value)
File "/home/mefioo/public_html/kariera_naukowa/env/lib/python2.7/site-packages/django/utils/encoding.py", line 102, in force_text
raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x268a9d0> (<class 'django.utils.functional.__proxy__'>)
我查了一下,0xc3 是 Ã 或 Ä 我当然不会使用(它甚至不存在于波兰字母中)。
通过的fk之一是
wydzial = models.ForeignKey(Wydzial, verbose_name="Wydział")
它包含波兰语 'ł',但一切正常。
我在这里错过了什么?
如果您在模型中使用 unicode 字符串,则应避免出现问题,例如verbose_name=u"Wydział"
。
您可能更愿意从未来的库中导入 unicode_literals,如 recommended by the Django docs。不过,如果将其添加到现有模块中,请小心!
from __future__ import unicode_literals
Unicode 与 python 字符串问题。 Django 文档已经朝着 python 3 的方向进一步发展,因此他们不再习惯于警告您了。在python2中,在字符串前面加上一个u,例如u"Wydział"
。由于您是 运行 2.7,您可以尝试 from __future__ import unicode_literals
。请注意,第二个解决方案可能会破坏您项目中的其他一些东西。