diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index ebb34101..7ec672fe 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -30,7 +30,7 @@ class BaseDocument(object): _dynamic_lock = True _initialised = False - def __init__(self, __auto_convert=True, *args, **values): + def __init__(self, *args, **values): """ Initialise a document or embedded document @@ -46,7 +46,7 @@ class BaseDocument(object): if name in values: raise TypeError("Multiple values for keyword argument '" + name + "'") values[name] = value - + __auto_convert = values.pop("__auto_convert", True) signals.pre_init.send(self.__class__, document=self, values=values) self._data = {} diff --git a/mongoengine/queryset/queryset.py b/mongoengine/queryset/queryset.py index c299190f..28a96189 100644 --- a/mongoengine/queryset/queryset.py +++ b/mongoengine/queryset/queryset.py @@ -1138,8 +1138,13 @@ class QuerySet(object): if self._hint != -1: self._cursor_obj.hint(self._hint) + return self._cursor_obj + def __deepcopy__(self, memo): + """Essential for chained queries with ReferenceFields involved""" + return self.clone() + @property def _query(self): if self._mongo_query is None: @@ -1302,6 +1307,9 @@ class QuerySet(object): except: pass key_list.append((key, direction)) + + if self._cursor_obj: + self._cursor_obj.sort(key_list) return key_list def _get_scalar(self, doc): diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index da0e89ab..1dccdb14 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -249,6 +249,10 @@ class QuerySetTest(unittest.TestCase): self.assertEqual(list(A.objects.none()), []) self.assertEqual(list(A.objects.none().all()), []) + def test_chaining(self): + class A(Document): + s = StringField() + class B(Document): ref = ReferenceField(A) boolfield = BooleanField(default=False) @@ -282,7 +286,7 @@ class QuerySetTest(unittest.TestCase): write_options = {"fsync": True} author, created = self.Person.objects.get_or_create( - name='Test User', write_options=write_options) + name='Test User', write_options=write_options) author.save(write_options=write_options) self.Person.objects.update(set__name='Ross', @@ -1475,7 +1479,6 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() - def test_set_list_embedded_documents(self): class Author(EmbeddedDocument): @@ -1533,11 +1536,11 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() blog_post_3 = BlogPost(title="Blog Post #3", - published_date=datetime(2010, 1, 6, 0, 0 ,0)) + published_date=datetime(2010, 1, 6, 0, 0, 0)) blog_post_2 = BlogPost(title="Blog Post #2", - published_date=datetime(2010, 1, 5, 0, 0 ,0)) + published_date=datetime(2010, 1, 5, 0, 0, 0)) blog_post_4 = BlogPost(title="Blog Post #4", - published_date=datetime(2010, 1, 7, 0, 0 ,0)) + published_date=datetime(2010, 1, 7, 0, 0, 0)) blog_post_1 = BlogPost(title="Blog Post #1", published_date=None) blog_post_3.save() @@ -1563,11 +1566,11 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() blog_post_1 = BlogPost(title="A", - published_date=datetime(2010, 1, 6, 0, 0 ,0)) + published_date=datetime(2010, 1, 6, 0, 0, 0)) blog_post_2 = BlogPost(title="B", - published_date=datetime(2010, 1, 6, 0, 0 ,0)) + published_date=datetime(2010, 1, 6, 0, 0, 0)) blog_post_3 = BlogPost(title="C", - published_date=datetime(2010, 1, 7, 0, 0 ,0)) + published_date=datetime(2010, 1, 7, 0, 0, 0)) blog_post_2.save() blog_post_3.save() @@ -1604,6 +1607,7 @@ class QuerySetTest(unittest.TestCase): qs = self.Person.objects.all().limit(10) qs = qs.order_by('-age') + ages = [p.age for p in qs] self.assertEqual(ages, [40, 30, 20])