From e38bf63be0c51c6243010f255bd45e9e2a8ddcec Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Thu, 24 Jan 2013 11:29:51 +0000 Subject: [PATCH] Fixed overriding objects with custom manager (#58) --- AUTHORS | 3 ++- docs/changelog.rst | 3 ++- mongoengine/document.py | 17 ++++++++--------- tests/queryset/queryset.py | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3f80dcae..c32ab9f9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -136,4 +136,5 @@ that much better: * Aleksey Porfirov * Nicolas Trippar * Manuel Hermann - * Gustavo Gawryszewski \ No newline at end of file + * Gustavo Gawryszewski + * Max Countryman \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index 219e935a..0d164b51 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -34,10 +34,11 @@ Changes in 0.8.X - Fixed validation for GenericReferences which havent been dereferenced - Added switch_db context manager (#106) - Added switch_db method to document instances (#106) -- Added no_dereference context manager (#82) +- Added no_dereference context manager (#82) (#61) - Added switch_collection context manager (#220) - Added switch_collection method to document instances (#220) - Added support for compound primary keys (#149) (#121) +- Fixed overriding objects with custom manager (#58) Changes in 0.7.9 ================ diff --git a/mongoengine/document.py b/mongoengine/document.py index 75873b4b..edc819cb 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -9,7 +9,7 @@ from mongoengine import signals from mongoengine.base import (DocumentMetaclass, TopLevelDocumentMetaclass, BaseDocument, BaseDict, BaseList, ALLOW_INHERITANCE, get_document) -from mongoengine.queryset import OperationError, NotUniqueError +from mongoengine.queryset import OperationError, NotUniqueError, QuerySet from mongoengine.connection import get_db, DEFAULT_CONNECTION_NAME from mongoengine.context_managers import switch_db, switch_collection @@ -328,10 +328,9 @@ class Document(BaseDocument): """ Returns the queryset to use for updating / reloading / deletions """ - qs = self.__class__.objects - if hasattr(self, '_objects'): - qs = self._objects - return qs + if not hasattr(self, '__objects'): + self.__objects = QuerySet(self, self._get_collection()) + return self.__objects @property def _object_key(self): @@ -394,8 +393,8 @@ class Document(BaseDocument): self._get_db = lambda: db self._collection = collection self._created = True - self._objects = self.__class__.objects - self._objects._collection_obj = collection + self.__objects = self._qs + self.__objects._collection_obj = collection return self def switch_collection(self, collection_name): @@ -419,8 +418,8 @@ class Document(BaseDocument): self._get_collection = lambda: collection self._collection = collection self._created = True - self._objects = self.__class__.objects - self._objects._collection_obj = collection + self.__objects = self._qs + self.__objects._collection_obj = collection return self def select_related(self, max_depth=1): diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index 35940447..0ad30927 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -2074,6 +2074,32 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() + def test_custom_manager_overriding_objects_works(self): + + class Foo(Document): + bar = StringField(default='bar') + active = BooleanField(default=False) + + @queryset_manager + def objects(doc_cls, queryset): + return queryset(active=True) + + @queryset_manager + def with_inactive(doc_cls, queryset): + return queryset(active=False) + + Foo.drop_collection() + + Foo(active=True).save() + Foo(active=False).save() + + self.assertEqual(1, Foo.objects.count()) + self.assertEqual(1, Foo.with_inactive.count()) + + Foo.with_inactive.first().delete() + self.assertEqual(0, Foo.with_inactive.count()) + self.assertEqual(1, Foo.objects.count()) + def test_query_value_conversion(self): """Ensure that query values are properly converted when necessary.