2013-12-12 16 views
0

我有一個模型老師有一個字段屬性。該屬性由一些預定義的選項填充。希臘的Unicode字符Django和管理後端

model.py

#-*-coding" utf-8 -*- 
field_options=[ 
    ('Greek letters', 'Greek letters'), 
    ('Greek letters', 'Greek letters'), 
    ('Greek letters', 'Greek letters'), 
    ('Greek letters', 'Greek letters'), 
] 

class Teacher(models.Model): 
    #fields here..... 
    field = models.CharField(choices=field_options, max_length=20) 

通過現場我的意思是一個老師可以是一個數學家一個physicst等,但我存儲這些字段的希臘字。當我去django的管理員後端存儲一名老師時,我使用希臘字母作爲他姓氏的老師姓名他的地址等我選擇了一個選項爲他的領域,但當我點擊保存它給了我一個錯誤,價值的字段不是一個有效的選擇,讓我當值的字段像

u'\u039c\u0391\u0398.... etc 

unicode的represantation爲什麼它保存在其他領域的其他希臘字母(姓氏名字等),而不是特定的一個?我能做些什麼來解決這個問題?

編輯:控制檯給我下面的錯誤。

/usr/local/lib/python2-7/dist-packages/django/db/models/fields/__init__.py:194: 
UnicodeWarning:Unicode equal comparison failed to convert both arguments to Unicode -interpreting them as being unequal 
    elif value == option_key 

還有一想....我改變了它syncdb.First後,它只是一個CharField沒有選擇,我手動添加它(無南).Could是這樣的錯嗎?

+0

怎麼來的? –

回答

2

嘗試添加u爲字符串元組:

(u'Greek letters', u'Greek letters'), 
#('Greek letters', 'Greek letters'), 

併爲您在元組max_length=20第一個元素...

+0

謝謝!!!它工作! – Apostolos

2

一般除了unicode和它的問題:

由於你使用Python2.7,你有時會遇到unicode問題。我強烈建議 花費30分鐘,以避免大部分未來的問題。看看下面的談話:

務實的Unicode,或者,我該如何阻止痛苦?你沒有使用`unicode`文字

http://pyvideo.org/video/948/

+1

謝謝你會考慮它 – Apostolos