Objects queryset manager now inherited (#256)
This commit is contained in:
parent
c16e6d74e6
commit
efad628a87
@ -4,6 +4,7 @@ Changelog
|
|||||||
|
|
||||||
Changes in 0.8.X
|
Changes in 0.8.X
|
||||||
================
|
================
|
||||||
|
- Objects manager now inherited (#256)
|
||||||
- Updated connection to use MongoClient (#262, #274)
|
- Updated connection to use MongoClient (#262, #274)
|
||||||
- Fixed db_alias and inherited Documents (#143)
|
- Fixed db_alias and inherited Documents (#143)
|
||||||
- Documentation update for document errors (#124)
|
- Documentation update for document errors (#124)
|
||||||
|
@ -315,8 +315,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
# may set allow_inheritance to False
|
# may set allow_inheritance to False
|
||||||
simple_class = all([b._meta.get('abstract')
|
simple_class = all([b._meta.get('abstract')
|
||||||
for b in flattened_bases if hasattr(b, '_meta')])
|
for b in flattened_bases if hasattr(b, '_meta')])
|
||||||
if (not simple_class and meta['allow_inheritance'] == False and
|
if (not simple_class and meta['allow_inheritance'] is False and
|
||||||
not meta['abstract']):
|
not meta['abstract']):
|
||||||
raise ValueError('Only direct subclasses of Document may set '
|
raise ValueError('Only direct subclasses of Document may set '
|
||||||
'"allow_inheritance" to False')
|
'"allow_inheritance" to False')
|
||||||
|
|
||||||
@ -339,9 +339,9 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
if callable(collection):
|
if callable(collection):
|
||||||
new_class._meta['collection'] = collection(new_class)
|
new_class._meta['collection'] = collection(new_class)
|
||||||
|
|
||||||
# Provide a default queryset unless one has been set
|
# Provide a default queryset unless exists or one has been set
|
||||||
manager = attrs.get('objects', QuerySetManager())
|
if not hasattr(new_class, 'objects'):
|
||||||
new_class.objects = manager
|
new_class.objects = QuerySetManager()
|
||||||
|
|
||||||
# Validate the fields and set primary key if needed
|
# Validate the fields and set primary key if needed
|
||||||
for field_name, field in new_class._fields.iteritems():
|
for field_name, field in new_class._fields.iteritems():
|
||||||
|
@ -2233,6 +2233,42 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertEqual(0, Foo.with_inactive.count())
|
self.assertEqual(0, Foo.with_inactive.count())
|
||||||
self.assertEqual(1, Foo.objects.count())
|
self.assertEqual(1, Foo.objects.count())
|
||||||
|
|
||||||
|
def test_inherit_objects(self):
|
||||||
|
|
||||||
|
class Foo(Document):
|
||||||
|
meta = {'allow_inheritance': True}
|
||||||
|
active = BooleanField(default=True)
|
||||||
|
|
||||||
|
@queryset_manager
|
||||||
|
def objects(klass, queryset):
|
||||||
|
return queryset(active=True)
|
||||||
|
|
||||||
|
class Bar(Foo):
|
||||||
|
pass
|
||||||
|
|
||||||
|
Bar.drop_collection()
|
||||||
|
Bar.objects.create(active=False)
|
||||||
|
self.assertEqual(0, Bar.objects.count())
|
||||||
|
|
||||||
|
def test_inherit_objects_override(self):
|
||||||
|
|
||||||
|
class Foo(Document):
|
||||||
|
meta = {'allow_inheritance': True}
|
||||||
|
active = BooleanField(default=True)
|
||||||
|
|
||||||
|
@queryset_manager
|
||||||
|
def objects(klass, queryset):
|
||||||
|
return queryset(active=True)
|
||||||
|
|
||||||
|
class Bar(Foo):
|
||||||
|
@queryset_manager
|
||||||
|
def objects(klass, queryset):
|
||||||
|
return queryset(active=False)
|
||||||
|
|
||||||
|
Bar.drop_collection()
|
||||||
|
Bar.objects.create(active=False)
|
||||||
|
self.assertEqual(0, Foo.objects.count())
|
||||||
|
self.assertEqual(1, Bar.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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user