我在App Engine數據存儲區中使用了Expando模型,並設置了許多任意屬性名稱。我沒有考慮到我無法存儲Unicode屬性名稱,現在我處於一種令人不安的情況,那就是試圖獲取這種實體,或者甚至刪除它們以擺脫犯罪者的錯誤:使用Unicode屬性名稱從Google App Engine數據庫中獲取或刪除實體
Traceback (most recent call last):
File "/base/data/home/apps/APP/1-05.335746938130078870/console/app/models/console.py", line 146, in processSource
exec bytecode in statement_module.__dict__
File "<string>", line 1, in <module>
File "/base/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1461, in fetch
return [self._model_class.from_entity(e) for e in raw]
File "/base/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1047, in from_entity
entity_values = cls._load_entity_values(entity)
File "/base/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1347, in _load_entity_values
entity_values[str(key)] = value
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 5: ordinal not in range(128)
現在我已經更改了我的代碼,以便在保存之前將這些屬性名稱編碼爲ascii,但已經太遲了!由於至少有一個實體具有Unicode類型的屬性名稱,因此我不可能獲取實體或將其刪除,這意味着數據存儲區查看器正在返回「發生服務器錯誤」。錯誤,所以我甚至無法看到問題出在哪裏。
唯一能想到的是分叉db模塊並更改str()方法,但對於我來說,找到一種方法來擦除這個模型甚至更好,能夠更容易地刪除違規實體(或多個實體)而不刪除所有有效的實體。
做到這一點的一種方法可能是獲取所有這些實體的關鍵字,然後執行try塊來查看是否可以獲取每個關鍵字,以及何時拋出異常,只需使用該關鍵字就可以刪除該實體。 – jamtoday 2009-08-20 22:20:39