2017-08-09 85 views
0

我會在下面發佈我的模型,但請注意,我在任何地方都使用'u'前綴,並且添加了魔術編碼行作爲我的文件的第二行。我使用Django 1.9和python 2.7運行makemigrations時DjangoUnicodeDecodeError

模型

#!/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命令後,它開始建立補充文件:

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__'>) 
+0

請顯示完整的跟蹤錯誤 –

+0

我已經添加了完整的跟蹤現在 – P4nd4b0b3r1n0

回答

0

嘗試用B更換你的U '...' '...'

我使用Django自動創建我的遷移(使用makemigrations),它傾向於默認這個。

+0

試過了,但它仍然給出了相同的錯誤 – P4nd4b0b3r1n0

0

您使用django翻譯,所以你不需要在verbose_namehelp_text裏面使用unicode,用英文替換這個文本然後嘗試。

相關問題