如何在谷歌应用引擎中识别具有损坏的参考属性的对象

时间:2012-06-06 08:16:15

标签: python google-app-engine object dereference

如何正确检查谷歌应用引擎中损坏的参考属性?

示例:

class User (db.Model):
    userName = db.StringProperty(multiline=False)
class Foo (db.Model):
    user = db.ReferenceProperty(User, collection_name="user_foo")
  1. 已创建用户对象。
  2. 创建了一个Foo对象。
  3. 然后删除User中相应的引用属性。

2 个答案:

答案 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)