Appengine反向引用问题
根据文档:http://code.google.com/ appengine/docs/python/datastore/datamodeling.html#References 自动创建的反向引用对象是一个 Query 对象,因此可能对其进行迭代并进行 fetch 调用。
但是: 我有一个模型:
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)
我收到这样的异常:
<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable
或这样:
<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch'
我做错了什么还是 appengine 发生了一些变化?我很确定以前它的工作方式就像查询一样。文档页面上甚至还有一个示例,显示了与我相同的用法:
for obj in obj1.secondmodel_set:
# ...
另外获取没有反向引用的查询可以正常工作:
things = Thing.all().filter('owner =', user)
According to the docs: http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References
the automatically created reverse reference object is a Query object, so there is possible iteration over it and making fetch calls.
But:
I have one model:
class User(db.Model):
name = db.StringProperty()
...
and second model:
class Thing(db.Model):
owner = db.ReferenceProperty(User)
...
And when I try to access reverse reference:
for thing in user.thing_set:
...
or:
user.thing_set.fetch(100)
I get an exception like this:
<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable
or like this:
<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch'
Am I doing something wrong or there was some change in appengine? I'm pretty sure that previously it worked like a Query. There is even an example on the docs page, that shows the same usage as mine:
for obj in obj1.secondmodel_set:
# ...
Additionaly getting the query without reverse reference works ok:
things = Thing.all().filter('owner =', user)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两种方法(迭代和获取)都应该有效。要进行调试,您可能需要记录(或打印):
只是为了查看对象包含的内容...这可能会提示您可能出现的问题。
一些想法:
Both methods (iterating and fetch) should work. To debug, you might want to log (or print):
just to see what the objects contain... and that might give you a hint of what could be going wrong.
A couple of ideas: