运行makemigrations时出现DjangoUnicodeDecodeError

时间:2017-08-09 13:45:36

标签: python django

我将在下面发布我的模型,但请注意我到处都使用'u'前缀,并且我添加了魔术编码行作为我文件的第二行。我正在使用django 1.9和python 2.7

MODEL

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Organisaatio(models.Model):
    organisaation_nimi = models.CharField(
        verbose_name=_(u"Organisaation nimi"),
        max_length=128,
        null=True,
        blank=True,
        help_text=_(u"Organisaatiosi nimi"),
        )

    organisaation_koko = models.IntegerField(
        verbose_name=_(u"Organisaation Koko"),
        null=True,
        blank=True,
        help_text=_(u"Arvio organisaatiosi jäsenmaräästä"),
        )

    kuvaus = models.TextField(
        verbose_name=_(u"Tehtävän kuvaus"),
        blank=True,
        default="",
        help_text=_(u"Kuvaile tehtävää"),
        )

    kesto = models.DecimalField(
        verbose_name=_(u"Tehtävään käytetty aika"),
        max_digits = 3,
        decimal_places=2,
        null=True,
        help_text=_(u"Arvioi tunneissa, kuinka kauan tehtävään käytetään keskimäärin aikaa"),
        )

    kertaa = models.IntegerField(
        verbose_name=_(u"Kertaa per kuukausi"),
        null=True,
        blank=True,
        help_text=_(u"Arvioi kuinka monta kertaa kuukauden aikana tehtävä tehdään")
        )


    def __unicode__(self):
        return self.organisaation_nimi

运行makemigrations命令后,它开始创建make文件:

 Migrations for 'app':
  0002_auto_20170809_1628.py:
    - Create model Kysymys
    - Add field kertaa to organisaatio
    - Add field kesto to organisaatio
    - Add field kuvaus to organisaatio
    - Alter field organisaation_koko on organisaatio
    - Alter field organisaation_nimi on organisaatio

然后吐出一个unicode错误:

django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 21: ordinal not in range(128). You passed in 
<django.utils.functional.__proxy__ object at 0x109ba0ad0> (<class 
'django.utils.functional.__proxy__'>)

我很困惑,因为我已经断言文件顶部的编码,而且我在任何地方都使用了u前缀。

谢谢你的时间!

修改

这是追溯:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 150, in handle
    self.write_migration_files(changes)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 178, in write_migration_files
    migration_string = writer.as_string()
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 167, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
    _write(arg_name, arg_value)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 76, in _write
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 357, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 433, in serialize
    return cls.serialize_deconstructed(path, args, kwargs)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 318, in serialize_deconstructed
    arg_string, arg_imports = cls.serialize(arg)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/db/migrations/writer.py", line 346, in serialize
    value = force_text(value)
  File "/Users/user/anaconda/envs/my_project/lib/python2.7/site-packages/django/utils/encoding.py", line 88, in force_text
    raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 21: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x109898b10> (<class 'django.utils.functional.__proxy__'>)

1 个答案:

答案 0 :(得分:0)

尝试更换你的&#39; ...&#39;与b&#39; ...&#39;

我使用django自动创建我的迁移(使用makemigrations),它往往默认为此。