2011-01-24 43 views
5

根据该文档: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) 

我得到这样一个例外:

<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable 

或像这样:

<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch' 

难道我做错了什么或有在AppEngine上一些变化?我很确定,以前它像查询一样工作。甚至有文档页面上的例子,显示了相同的使用防雷:

for obj in obj1.secondmodel_set: 
    # ... 

Additionaly越来越无反向引用查询工作正常:

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

能您发现任何特定的环境下那会引起第一个例外,这会导致第二个例外? – 2011-01-24 18:45:33

回答

1

两种方法(迭代和提取)应工作。为了调试,你可能要记录(或打印):

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

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

只是为了看看对象包含......,也许他能给你什么可能会错误的暗示。

一对夫妇的想法: