反参考问题

2024-09-30 22:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

根据文件:http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References 自动创建的反向引用对象是一个查询对象,因此可能会对其进行迭代并进行fetch调用。在

但是: 我有一个型号:

class User(db.Model):
   name = db.StringProperty()
   ...

第二种型号:

^{pr2}$

当我尝试反向访问时:

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'

是我做错了什么事,还是外观有些变化?我很确定以前它就像一个查询。在docs页面上甚至有一个例子显示了与我相同的用法:

for obj in obj1.secondmodel_set:
    # ...

另外,在没有反向引用的情况下获取查询也可以:

things = Thing.all().filter('owner =', user)

Tags: 文件对象indocsfordbobjecttype
1条回答
网友
1楼 · 发布于 2024-09-30 22:13:44

这两种方法(迭代和获取)都可以工作。要调试,您可能需要记录(或打印):

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

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

只是想看看对象包含什么。。。这可能会给你一个可能出问题的线索。在

一些想法:

相关问题 更多 >