Fixed overriding objects with custom manager (#58)
This commit is contained in:
parent
e7ba5eb160
commit
e38bf63be0
1
AUTHORS
1
AUTHORS
@ -137,3 +137,4 @@ that much better:
|
||||
* Nicolas Trippar
|
||||
* Manuel Hermann
|
||||
* Gustavo Gawryszewski
|
||||
* Max Countryman
|
@ -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
|
||||
================
|
||||
|
@ -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):
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user