2011-01-24 45 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', ...] 

只是爲了看看對象包含......,也許他能給你什麼可能會錯誤的暗示。

一對夫婦的想法: