Nicolas Ferrari
Nicolas Ferrari

Reputation: 43

Mongoengine chaining filter() and ReferenceField() causes "TypeError: 'Collection' object is not callable"

Having these 2 MongoEngine Documents:

class A(Document):
    a = StringField()

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

I'd like first to filter() on a specific A object, and then, from the first query, filter() on the BooleanField. But these lines cause an error:

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

The error is:

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.

See the full code and traceback here: https://gist.github.com/nferrari/4962245

Thanks!

Upvotes: 1

Views: 1304

Answers (1)

Ross
Ross

Reputation: 18111

Seems that querying reference fields can't be chained in 0.7.8 - so for the time being please use a dictionary and then pass in as kwargs as a work round eg:

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

I have added: https://github.com/MongoEngine/mongoengine/issues/234 to be fixed in 0.8

Upvotes: 1

Related Questions