Paw*_*ski 5 python google-app-engine google-cloud-datastore
根据文档:http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References 自动创建的反向引用对象是一个Query对象,因此可能会对其进行迭代并进行提取调用.
但是:我有一个型号:
class User(db.Model):
name = db.StringProperty()
...
Run Code Online (Sandbox Code Playgroud)
和第二个模型:
class Thing(db.Model):
owner = db.ReferenceProperty(User)
...
Run Code Online (Sandbox Code Playgroud)
当我尝试访问反向引用时:
for thing in user.thing_set:
...
Run Code Online (Sandbox Code Playgroud)
要么:
user.thing_set.fetch(100)
Run Code Online (Sandbox Code Playgroud)
我得到这样的例外:
<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable
Run Code Online (Sandbox Code Playgroud)
或者像这样:
<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch'
Run Code Online (Sandbox Code Playgroud)
我做错了什么或者有什么改变吗?我很确定以前它像Query一样工作.文档页面上甚至有一个示例,显示与我的相同用法:
for obj in obj1.secondmodel_set:
# ...
Run Code Online (Sandbox Code Playgroud)
另外,没有反向引用的查询工作正常:
things = Thing.all().filter('owner =', user)
Run Code Online (Sandbox Code Playgroud)
两种方法(迭代和获取)都应该有效。要调试,您可能需要记录(或打印):
print dir(user)
[..., 'thing_set', ...]
print dir(user.thing_set)
[..., '__iter__', ... , 'fetch', ...]
Run Code Online (Sandbox Code Playgroud)
只是为了看看这些对象包含什么......这可能会给您一些可能出现问题的提示。
几个想法:
| 归档时间: |
|
| 查看次数: |
421 次 |
| 最近记录: |