您如何正确检查谷歌应用引擎中损坏的参考属性?
例子:
class User (db.Model):
userName = db.StringProperty(multiline=False)
class Foo (db.Model):
user = db.ReferenceProperty(User, collection_name="user_foo")
- 创建了一个用户对象。
- 创建了一个 Foo 对象。
- 然后删除 User 中相应的引用属性。
您如何正确检查谷歌应用引擎中损坏的参考属性?
例子:
class User (db.Model):
userName = db.StringProperty(multiline=False)
class Foo (db.Model):
user = db.ReferenceProperty(User, collection_name="user_foo")
正如丹尼尔罗斯曼在评论中所建议的那样:
“遍历所有 Foos 并访问 item.user,并 [检查] 是否引发了 ResolveError”
from google.appengine.api import datastore_errors
all_foo = Foo.all()
for bar in all_foo:
try:
user_refProperty = bar.user
except datastore_errors.Error, e:
if e.args[0][0:40] == "ReferenceProperty failed to be resolved:":
bar.delete()
self.response.out.write('deleted due to bad reference property')
else: raise
您可以在user
字段上查询:
referenced_users = Foo.all().filter('user', deleted_user_key)