Mongoengine链接过滤器()和ReferenceField()导致“TypeError:'Collection'对象不可调用”

时间:2013-02-15 19:05:26

标签: python mongodb pymongo mongoengine

拥有这两份MongoEngine文件:

class A(Document):
    a = StringField()

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

我首先想要特定A对象上的filter(),然后从第一个查询开始,在BooleanField上filter()。但这些行会导致错误:

a_objects = A.objects(a='test') # OK
query = B.objects(ref__in=a_objects) # OK
query2 = query.filter(boolfield=True) # FAILS

错误是:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

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

    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