gpt4 book ai didi

python - Appengine反向引用问题

转载 作者:太空狗 更新时间:2023-10-30 01:35:23 25 4
gpt4 key购买 nike

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

是我做错了什么还是应用引擎发生了一些变化?我很确定以前它像查询一样工作。文档页面上什至有一个示例,显示了与我相同的用法:

for obj in obj1.secondmodel_set:
# ...

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

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

最佳答案

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

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

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

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

几个想法:

关于python - Appengine反向引用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4780653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com