Appengine反向引用问题

发布于 2024-10-14 06:23:55 字数 1161 浏览 2 评论 0原文

根据文档: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笛声青案梦长安 2024-10-21 06:23:55

两种方法(迭代和获取)都应该有效。要进行调试,您可能需要记录(或打印):

print dir(user)
[..., 'thing_set', ...]

print dir(user.thing_set)
[..., '__iter__', ... , 'fetch', ...]

只是为了查看对象包含的内容...这可能会提示您可能出现的问题。

一些想法:

Both methods (iterating and fetch) should work. To debug, you might want to log (or print):

print dir(user)
[..., 'thing_set', ...]

print dir(user.thing_set)
[..., '__iter__', ... , 'fetch', ...]

just to see what the objects contain... and that might give you a hint of what could be going wrong.

A couple of ideas:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文