Mongoengine chaining filter()和ReferenceField()导致“TypeError:'Collection'对象不可调用”

2024-09-28 20:54:23 发布

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

有两份MongoEngine文件:

class A(Document):
    a = StringField()

class B(Document):
    b = StringField()
    boolfield = BooleanField(default=False)
    ref = ReferenceField(A)

我希望首先对特定的a对象执行filter(),然后,在第一个查询中,filter()在布尔字段上。但这些线会导致错误:

^{pr2}$

错误是:

TypeError: 'Collection' object is not callable. If you meant to call the '__deepcopy__' method on a 'Collection' object it is failing because no such method exists.

在这里查看完整的代码和回溯:https://gist.github.com/nferrari/4962245

谢谢!在


Tags: 文件falsedefaultobjectis错误filterdocument
1条回答
网友
1楼 · 发布于 2024-09-28 20:54:23

在0.7.8中,查询引用字段似乎不能链接起来,所以暂时请使用字典,然后作为工作循环传入,例如:

    a_objects = A.objects(a='test')
    query_dict = {'ref__in': a_objects}
    query_dict['boolfield'] = True
    self.assertEquals(B.objects(**query_dict).count(), 1)

我添加了:https://github.com/MongoEngine/mongoengine/issues/234将在0.8中修复

相关问题 更多 >