如何正确检查谷歌应用引擎中损坏的参考属性?
示例:
class User (db.Model):
userName = db.StringProperty(multiline=False)
class Foo (db.Model):
user = db.ReferenceProperty(User, collection_name="user_foo")
答案 0 :(得分:1)
正如Daniel Roseman在评论中所建议的那样:
“遍历所有Food并访问item.user,并[检查]是否引发了Resolve Error”
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
答案 1 :(得分:0)
您只需查询user
字段:
referenced_users = Foo.all().filter('user', deleted_user_key)