Skip to content

Instantly share code, notes, and snippets.

@nferrari
Created February 15, 2013 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nferrari/4962245 to your computer and use it in GitHub Desktop.
Save nferrari/4962245 to your computer and use it in GitHub Desktop.
from mongoengine import Document, StringField, ReferenceField, BooleanField
class A(Document):
a = StringField()
class B(Document):
b = StringField()
boolfield = BooleanField(default=False)
ref = ReferenceField(A)
A1 = A(a='abcde')
A1.save()
<A: A object>
A2 = A(a='xyz')
A2.save()
<A: A object>
B11 = B(b='bcd', ref=A1)
B11.save()
<B: B object>
B12 = B(b='bbb', boolfield=True, ref=A1)
B12.save()
<B: B object>
B21 = B(b='zzz', ref=A2)
B21.save()
<B: B object>
B22 = B(b='yyy', boolfield=True, ref=A2)
B22.save()
<B: B object>
a_query = A.objects(a='abcde')
a_query
[<A: A object>]
b_query = B.objects(ref__in=a_query)
b_query
[<B: B object>, <B: B object>]
b_query_bool = b_query.filter(boolfield=True)
b_query_bool
/dev/mongoengine/queryset.pyc in __repr__(self)
1968 for i in xrange(REPR_OUTPUT_SIZE + 1):
1969 try:
-> 1970 data.append(self.next())
1971 except StopIteration:
1972 break
/dev/mongoengine/queryset.pyc in next(self)
1013 return self._get_as_pymongo(self._cursor.next())
1014
-> 1015 return self._document._from_son(self._cursor.next())
1016 except StopIteration, e:
1017 self.rewind()
/dev/mongoengine/queryset.pyc in _cursor(self)
605 if self._cursor_obj is None:
606
--> 607 self._cursor_obj = self._collection.find(self._query,
608 **self._cursor_args)
609 # Apply where clauses to cursor
/dev/mongoengine/queryset.pyc in _query(self)
387 def _query(self):
388 if self._mongo_query is None:
--> 389 self._mongo_query = self._query_obj.to_query(self._document)
390 if self._class_check:
391 self._mongo_query.update(self._initial_query)
/dev/mongoengine/queryset.pyc in to_query(self, document)
208
209 def to_query(self, document):
--> 210 query = self.accept(SimplificationVisitor())
211 query = query.accept(QueryTreeTransformerVisitor())
212 query = query.accept(QueryCompilerVisitor(document))
/dev/mongoengine/queryset.pyc in accept(self, visitor)
259 self.children[i] = self.children[i].accept(visitor)
260
--> 261 return visitor.visit_combination(self)
262
263 @property
/dev/mongoengine/queryset.pyc in visit_combination(self, combination)
78 if all(isinstance(node, Q) for node in combination.children):
79 queries = [node.query for node in combination.children]
---> 80 return Q(**self._query_conjunction(queries))
81 return combination
82
/dev/mongoengine/queryset.pyc in _query_conjunction(self, queries)
96
97 query_ops.update(ops)
---> 98 combined_query.update(copy.deepcopy(query))
99 return combined_query
100
/usr/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161 copier = _deepcopy_dispatch.get(cls)
162 if copier:
--> 163 y = copier(x, memo)
164 else:
165 try:
/usr/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255 memo[id(x)] = y
256 for key, value in x.iteritems():
--> 257 y[deepcopy(key, memo)] = deepcopy(value, memo)
258 return y
259 d[dict] = _deepcopy_dict
/usr/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188 raise Error(
189 "un(deep)copyable object of type %s" % cls)
--> 190 y = _reconstruct(x, rv, 1, memo)
191
192 memo[d] = y
/usr/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332 if state:
333 if deep:
--> 334 state = deepcopy(state, memo)
335 if hasattr(y, '__setstate__'):
336 y.__setstate__(state)
/usr/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161 copier = _deepcopy_dispatch.get(cls)
162 if copier:
--> 163 y = copier(x, memo)
164 else:
165 try:
/usr/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255 memo[id(x)] = y
256 for key, value in x.iteritems():
--> 257 y[deepcopy(key, memo)] = deepcopy(value, memo)
258 return y
259 d[dict] = _deepcopy_dict
/usr/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
172 copier = getattr(x, "__deepcopy__", None)
173 if copier:
--> 174 y = copier(memo)
175 else:
176 reductor = dispatch_table.get(cls)
/envs/alwaysdata/lib/python2.7/site-packages/pymongo/collection.pyc in __call__(self, *args, **kwargs)
1401 self.__name)
1402 raise TypeError("'Collection' object is not callable. If you meant to "
1403 "call the '%s' method on a 'Collection' object it is "
1404 "failing because no such method exists." %
-> 1405 self.__name.split(".")[-1])
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment