2012-02-10 11 views
3

Pythonの開発者として、私は何年もPythonのUnicodeの問題を抱えていました。 しかし、今私はナッツを運転する状況があり、私はそれを自分で解決することはできません。Django&Suds:QuerySets使用時のUnicodeEncodeError

: それはすでに私のセットアップはいくつかのデータを引き出し、Djangoのデータベースにそれを探して、(泡を使用して)SOAP経由でリモートシステムに接続する小さなDjangoアプリケーション..ですrecherches含めて、今1日

を取りました

from myapp.models import Customer 
client = suds.client.Client(...) 
customer = client.service.getCustomerByEmail('[email protected]') 

type(customer.email): <class 'suds.sax.text.Text'> 

customer_exists = Customer.objects.filter(email=customer.email) 
今すぐお客様のメールアドレスは次のようにDjangoは例外を発生させることができますドイツ語のウムラウトのü、があります。

Traceback (most recent call last): 
    File "run_anatomy_client.py", line 19, in <module> 
    print client.main() 
    File "/Users/user/Documents/workspace/Wawi/application/myapp/client.py", line 282, in main 
    if not Customer.objects.filter(email=customer.email.encode('latin1')): 
    File "/Users/user/Documents/workspace/Wawi/application/myapp/client.py", line 76, in sync_customer 
    if not customer_exists: 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 113, in __nonzero__ 
    iter(self).next() 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 107, in _result_iter 
    self._fill_cache() 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 772, in _fill_cache 
    self._result_cache.append(self._iter.next()) 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 273, in iterator 
    for row in compiler.results_iter(): 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/backends/util.py", line 43, in execute 
    logger.debug('(%.3f) %s; args=%s' % (duration, sql, params), 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 28: ordinal not in range(128) 

私はすでにエンコード()、デコード()と共演し、ソースファイルのコーディングを変更データベースl次のように現在見えている、ayout:

mysql> show variables like '%character%'; 
+--------------------------+-----------------------------------------+ 
| Variable_name   | Value         | 
+--------------------------+-----------------------------------------+ 
| character_set_client  | latin1         | 
| character_set_connection | latin1         | 
| character_set_database | utf8         | 
| character_set_filesystem | binary         | 
| character_set_results | latin1         | 
| character_set_server  | latin1         | 
| character_set_system  | utf8         | 
| character_sets_dir  | /opt/local/share/mysql5/mysql/charsets/ | 
+--------------------------+-----------------------------------------+ 
8 rows in set (0.00 sec) 

奇妙なことがある - 私は、トレースポイントを設定し、Djangoのシェルでまったく同じ行を実行した場合、エンコードを使用した場合、それだけで正常に動作します():

(Pdb) Customer.objects.filter(email=customer.email) 
*** UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 28:  ordinal not in range(128) 
(Pdb) Customer.objects.filter(email=customer.email.encode('utf-8')) 
[] 

私はすべてのヒントに感謝するだろう。..

+0

こんにちは、ユニコード(customer.email、 'iso8859-1')を試すことができます、それが助けてくれたら教えてください... – Jingo

+0

Th Customer.objects.filter(email = unicode(customer.email、 'iso8859-1')): TypeError:Unicodeのデコードがサポートされていません –

+0

問題を解決できましたか?私は同じ問題に直面しているように見える – Fedor

答えて

2

suds.sax.text.Textはユニコードから継承

class Text(unicode): 
    """ 
    An XML text object used to represent text content. 
    @ivar lang: The (optional) language flag. 
    @type lang: bool 
    @ivar escaped: The (optional) XML special character escaped flag. 
    @type escaped: bool 
    """ 

作業する場合は、UTF-8にエンコードするだけで済みます。

email = customer.email.encode("utf-8") 
customer_exists = Customer.objects.filter(email=email) 
+0

私は、customer.encode( 'utf-8')の代わりにunicode(customer)を使用するほうがよいと思う。 – Fedor

+0

私は*全く*同じ問題を抱えていた。これは本当にありがとう! –

0

私は何が起こっているかを把握しようと2時間以上を費やし、私は保存することができない理由Djangoはオブジェクトのフィールドに泡のデータ構造からの値を代入した後、オブジェクト。

@guillaumevincentによると、Suds Textクラスはunicodeから継承されており、実装は100%正確ではないため、Djangoは基本ユニコードタイプで動作するいくつかの操作を実行した後に失敗します。

そこで質問から例えば私は

customer_exists = Customer.objects.filter(email=unicode(customer.email))

を行うだろうと私の場合、私はうまくいけば、これが誰かのためにいくつかの時間を節約する同様

django_obj.field_name = suds_obj.field_name

それをやった:)

+0

unicode()はPython 3では定義されていません。私の解決策はPython 2.7とPython 3で動作します。 –

関連する問題