Fixed overriding objects with custom manager (#58)
This commit is contained in:
		
							
								
								
									
										3
									
								
								AUTHORS
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								AUTHORS
									
									
									
									
									
								
							| @@ -136,4 +136,5 @@ that much better: | |||||||
|  * Aleksey Porfirov |  * Aleksey Porfirov | ||||||
|  * Nicolas Trippar |  * Nicolas Trippar | ||||||
|  * Manuel Hermann |  * Manuel Hermann | ||||||
|  * Gustavo Gawryszewski |  * Gustavo Gawryszewski | ||||||
|  |  * Max Countryman | ||||||
| @@ -34,10 +34,11 @@ Changes in 0.8.X | |||||||
| - Fixed validation for GenericReferences which havent been dereferenced | - Fixed validation for GenericReferences which havent been dereferenced | ||||||
| - Added switch_db context manager (#106) | - Added switch_db context manager (#106) | ||||||
| - Added switch_db method to document instances (#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 context manager (#220) | ||||||
| - Added switch_collection method to document instances (#220) | - Added switch_collection method to document instances (#220) | ||||||
| - Added support for compound primary keys (#149) (#121) | - Added support for compound primary keys (#149) (#121) | ||||||
|  | - Fixed overriding objects with custom manager (#58) | ||||||
|  |  | ||||||
| Changes in 0.7.9 | Changes in 0.7.9 | ||||||
| ================ | ================ | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ from mongoengine import signals | |||||||
| from mongoengine.base import (DocumentMetaclass, TopLevelDocumentMetaclass, | from mongoengine.base import (DocumentMetaclass, TopLevelDocumentMetaclass, | ||||||
|                               BaseDocument, BaseDict, BaseList, |                               BaseDocument, BaseDict, BaseList, | ||||||
|                               ALLOW_INHERITANCE, get_document) |                               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.connection import get_db, DEFAULT_CONNECTION_NAME | ||||||
| from mongoengine.context_managers import switch_db, switch_collection | 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 |         Returns the queryset to use for updating / reloading / deletions | ||||||
|         """ |         """ | ||||||
|         qs = self.__class__.objects |         if not hasattr(self, '__objects'): | ||||||
|         if hasattr(self, '_objects'): |             self.__objects = QuerySet(self, self._get_collection()) | ||||||
|             qs = self._objects |         return self.__objects | ||||||
|         return qs |  | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def _object_key(self): |     def _object_key(self): | ||||||
| @@ -394,8 +393,8 @@ class Document(BaseDocument): | |||||||
|         self._get_db = lambda: db |         self._get_db = lambda: db | ||||||
|         self._collection = collection |         self._collection = collection | ||||||
|         self._created = True |         self._created = True | ||||||
|         self._objects = self.__class__.objects |         self.__objects = self._qs | ||||||
|         self._objects._collection_obj = collection |         self.__objects._collection_obj = collection | ||||||
|         return self |         return self | ||||||
|  |  | ||||||
|     def switch_collection(self, collection_name): |     def switch_collection(self, collection_name): | ||||||
| @@ -419,8 +418,8 @@ class Document(BaseDocument): | |||||||
|         self._get_collection = lambda: collection |         self._get_collection = lambda: collection | ||||||
|         self._collection = collection |         self._collection = collection | ||||||
|         self._created = True |         self._created = True | ||||||
|         self._objects = self.__class__.objects |         self.__objects = self._qs | ||||||
|         self._objects._collection_obj = collection |         self.__objects._collection_obj = collection | ||||||
|         return self |         return self | ||||||
|  |  | ||||||
|     def select_related(self, max_depth=1): |     def select_related(self, max_depth=1): | ||||||
|   | |||||||
| @@ -2074,6 +2074,32 @@ class QuerySetTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         BlogPost.drop_collection() |         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): |     def test_query_value_conversion(self): | ||||||
|         """Ensure that query values are properly converted when necessary. |         """Ensure that query values are properly converted when necessary. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user