DjangoUnicodeDecodeError升级到1.8后的makemigrations

时间:2015-07-15 15:28:06

标签: django unicode django-south django-migrations django-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是Ã或Ä我当然不使用(它甚至不存在于波兰语字母表中)。

传递的其中一个是

wydzial = models.ForeignKey(Wydzial, verbose_name="Wydział")

它包含波兰'ł',但一切都很好。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

如果在模型中使用unicode字符串,则应避免出现问题,例如: verbose_name=u"Wydział"

您可能更愿意从未来的库中导入unicode_literals,如recommended by the Django docs。如果将其添加到现有模块,请小心!

from __future__ import unicode_literals

答案 1 :(得分:0)

Unicode vs python字符串问题。 Django文档已经向python 3的方向发展,所以他们不再习惯警告你了。在python2中,将一个u放在字符串前面,例如u"Wydział"。由于您运行的是2.7,因此可以尝试from __future__ import unicode_literals。请注意,第二个解决方案可能会破坏项目中的其他一些内容。