2009-08-20 18 views
1

我在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()方法,但對於我來說,找到一種方法來擦除這個模型甚至更好,能夠更容易地刪除違規實體(或多個實體)而不刪除所有有效的實體。

+0

做到這一點的一種方法可能是獲取所有這些實體的關鍵字,然後執行try塊來查看是否可以獲取每個關鍵字,以及何時拋出異常,只需使用該關鍵字就可以刪除該實體。 – jamtoday 2009-08-20 22:20:39

回答

1

我留下的評論結束了工作。

這裏的解決方案(假設有這樣的實體的1000下):

entities = db.GqlQuery("SELECT __key__ FROM ExpandoModel").fetch(1000) 

for e in entities: 

    try: db.get(e) 
    except UnicodeEncodeError: db.delete(e) 
1

對於未來的參考,也可以導入和使用google.appengine.api.datastore模塊,它提供了一個較低基於字典的基於接口的數據存儲。

您可能也想對此file a bug

+0

感謝您的提示。我提出了一個問題:http://code.google.com/p/googleappengine/issues/detail?id=2007 – jamtoday 2009-08-21 07:04:23

相關問題