根据文档:http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References,自动生成的反向引用对象是一个查询对象,因此可以对其进行迭代和调用获取函数。
但是: 我有一个模型:
第二个模型:
或者像这样:
但是: 我有一个模型:
class User(db.Model):
name = db.StringProperty()
...
第二个模型:
class Thing(db.Model):
owner = db.ReferenceProperty(User)
...
当我尝试访问反向引用时:
for thing in user.thing_set:
...
或者:
user.thing_set.fetch(100)
I get an exception like this:
<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable
或者像这样:
<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch'
我做错了什么还是App Engine发生了一些变化? 我非常确定以前它的工作方式就像一个Query。 甚至在文档页面上有一个示例,展示了与我的用法相同的用例:
for obj in obj1.secondmodel_set:
# ...
另外,获取没有反向引用的查询也可以正常工作:
things = Thing.all().filter('owner =', user)