Google app engine 使用Unicode属性名从Google App Engine数据库获取或删除实体

Google app engine 使用Unicode属性名从Google App Engine数据库获取或删除实体,google-app-engine,unicode,model,Google App Engine,Unicode,Model,我的应用程序引擎数据存储中有一个Expando模型类型,我正在设置许多任意属性名称。我不认为我不能存储Unicode属性名,现在我遇到了一个麻烦的情况,即任何试图获取这种类型的实体,或者甚至删除它们来除掉违法者都会得到以下错误: Traceback (most recent call last): File "/base/data/home/apps/APP/1-05.335746938130078870/console/app/models/console.py", line 146, i

我的应用程序引擎数据存储中有一个Expando模型类型,我正在设置许多任意属性名称。我不认为我不能存储Unicode属性名,现在我遇到了一个麻烦的情况,即任何试图获取这种类型的实体,或者甚至删除它们来除掉违法者都会得到以下错误:

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)
回溯(最近一次呼叫最后一次):
processSource中的文件“/base/data/home/apps/APP/1-05.335746938130078870/console/APP/models/console.py”,第146行
语句模块中的exec字节码__
文件“”,第1行,在
文件“/base/python_lib/versions/1/google/appengine/ext/db/_init__.py”,第1461行,在fetch中
返回[self.\u model\u class.from_entity(e)for e in raw]
文件“/base/python_-lib/versions/1/google/appengine/ext/db/_-init__uuu.py”,第1047行,在from_-entity中
实体\u值=cls.\u加载\u实体\u值(实体)
文件“/base/python_lib/versions/1/google/appengine/ext/db/_init__.py”,第1347行,在加载实体值中
实体_值[str(键)]=值
UnicodeEncodeError:“ascii”编解码器无法对位置5中的字符u'\xe1'进行编码:序号不在范围内(128)
现在,我已经更改了代码,在保存这些属性名称之前将其编码为ascii,但为时已晚!因为至少有一个实体具有Unicode类型的属性名,所以我无法获取或删除该实体,这意味着数据存储查看器返回“发生了服务器错误”。错误,因此我甚至看不到问题出在哪里


我能想到的唯一办法是分叉db模块并更改str()方法,但是我想办法删除这个模型会容易得多,甚至更好,能够删除有问题的实体而不删除所有有效的实体

我留下的评论最终奏效了

以下是解决方案(假设此类实体少于1000家):


为了便于将来参考,您还可以导入并使用google.appengine.api.datastore模块,该模块为数据存储提供了一个较低级别、基于dict的接口


您可能还想了解这一点。

一种方法可能是获取所有这些实体的键,然后执行一个try块以查看是否可以获取每个实体,并在引发异常时使用该键删除该实体。谢谢您的提示。我提出了一个问题:
entities = db.GqlQuery("SELECT __key__ FROM ExpandoModel").fetch(1000)

for e in entities:

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