2011-01-22 27 views
1

我想將一個字符串保存到數據庫中,其中包含字符的序號不在範圍內(128)。該字段聲明爲文本字段的模型,當我打電話給save()方法,引發了異常:TextField在Django中採用unicode

File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 434, in save self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 500, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values) 
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 491, in _update return query.get_compiler(self.db).execute_sql(None) 
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 861, in execute_sql cursor = super(SQLUpdateCompiler, self).execute_sql(result_type) 
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 717, in execute_sql sql, params = self.as_sql() 
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 826, in as_sql val = field.get_db_prep_save(val, connection=self.connection) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 28, in inner return func(*args, **kwargs) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 28, in inner return func(*args, **kwargs) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 276, in get_db_prep_save return self.get_db_prep_value(value, connection=connection, prepared=False) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 53, in inner return func(*args, **kwargs) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 652, in get_db_prep_value value = self.get_prep_value(value) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 647, in get_prep_value return self.to_python(value) 
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 610, in to_python if not ansi_date_re.search(value): 
TypeError: expected string or buffer 

這裏是我做過什麼:

str_unicode = str.encode('utf-8') 
m = MyModel.new(str = str_unicode) 
m.save() 

所以,我應該怎麼做才能使unicode兼容性?

更新:我目前使用的發展,蟒蛇2.6.1和1.2.4的Django的sqlite3在Mac

回答

0

原因是我沒有在django的數據庫配置中加載mysql設置,這與我提出的unicode問題無關。

的配置應該是這樣的:

DATABASES = { 
     'default': { 
      'ENGINE': 'django.db.backends.mysql', 
      'NAME': 'db', 
      'USER': 'xxx', 
      'PASSWORD': 'xxx', 
      'HOST': 'localhost', 
      'PORT': '3306', 
      'OPTIONS' : { 
       "init_command": "SET storage_engine=INNODB", 
       'read_default_file': '/etc/my.cnf', 
      } 
     } 
    } 
0

Django的正常工作與統一。

但是,如果您的輸入未正確轉換,您將遇到麻煩。 作爲一個例子:我用一個錯誤的php腳本錯誤地填充了一箇舊的MyISAM數據庫,遇到了很多麻煩。 檢查您的輸入。

你使用什麼數據庫後端?什麼級別的Python?

+0

我目前使用的sqlite3在Mac開發和Python 2.6.1。 – zsong 2011-01-22 20:47:38

3

utf-8 is not unicode!這條線:

str_unicode = str.encode('utf-8') 

做你打算什麼相反。它需要unicode,並將其轉換爲utf-8。不要這樣做。

+0

100%同意,但是從閱讀回溯結果來看,他的問題與TextField根本沒有任何關係。大聲笑... – 2011-01-22 23:21:56

2

你的問題根本就不在TextField中。如果您查看回溯中引用的代碼,則會觸發DateFieldto_python方法中的例外。你可以see the relevant code here。基本上,它看起來像是將不適當的東西作爲輸入傳遞給模型上的另一個字段。

要汲取的教訓:始終仔細閱讀跟蹤!

作爲一個側面說明,如果你真的想要挾的東西在Python中的Unicode調用內置unicode類型:

str_unicode = unicode(str) 

使用encode方法轉換爲UTF-8沒有完全不同的東西。