Merge pull request #253 from wpjunior/listreferences
Dereference fails (allow_inheritance = False)
This commit is contained in:
commit
2b1eb620fc
@ -3,6 +3,7 @@ import operator
|
|||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
from base import BaseDict, BaseList, get_document, TopLevelDocumentMetaclass
|
from base import BaseDict, BaseList, get_document, TopLevelDocumentMetaclass
|
||||||
|
from fields import ReferenceField
|
||||||
from connection import _get_db
|
from connection import _get_db
|
||||||
from queryset import QuerySet
|
from queryset import QuerySet
|
||||||
from document import Document
|
from document import Document
|
||||||
@ -32,8 +33,16 @@ class DeReference(object):
|
|||||||
items = [i for i in items]
|
items = [i for i in items]
|
||||||
|
|
||||||
self.max_depth = max_depth
|
self.max_depth = max_depth
|
||||||
|
|
||||||
|
doc_type = None
|
||||||
|
if instance and instance._fields:
|
||||||
|
doc_type = instance._fields[name].field
|
||||||
|
|
||||||
|
if isinstance(doc_type, ReferenceField):
|
||||||
|
doc_type = doc_type.document_type
|
||||||
|
|
||||||
self.reference_map = self._find_references(items)
|
self.reference_map = self._find_references(items)
|
||||||
self.object_map = self._fetch_objects()
|
self.object_map = self._fetch_objects(doc_type=doc_type)
|
||||||
return self._attach_objects(items, 0, instance, name, get)
|
return self._attach_objects(items, 0, instance, name, get)
|
||||||
|
|
||||||
def _find_references(self, items, depth=0):
|
def _find_references(self, items, depth=0):
|
||||||
@ -80,7 +89,7 @@ class DeReference(object):
|
|||||||
depth += 1
|
depth += 1
|
||||||
return reference_map
|
return reference_map
|
||||||
|
|
||||||
def _fetch_objects(self):
|
def _fetch_objects(self, doc_type=None):
|
||||||
"""Fetch all references and convert to their document objects
|
"""Fetch all references and convert to their document objects
|
||||||
"""
|
"""
|
||||||
object_map = {}
|
object_map = {}
|
||||||
@ -94,7 +103,10 @@ class DeReference(object):
|
|||||||
else: # Generic reference: use the refs data to convert to document
|
else: # Generic reference: use the refs data to convert to document
|
||||||
references = _get_db()[col].find({'_id': {'$in': refs}})
|
references = _get_db()[col].find({'_id': {'$in': refs}})
|
||||||
for ref in references:
|
for ref in references:
|
||||||
|
if '_cls' in ref:
|
||||||
doc = get_document(ref['_cls'])._from_son(ref)
|
doc = get_document(ref['_cls'])._from_son(ref)
|
||||||
|
else:
|
||||||
|
doc = doc_type._from_son(ref)
|
||||||
object_map[doc.id] = doc
|
object_map[doc.id] = doc
|
||||||
return object_map
|
return object_map
|
||||||
|
|
||||||
|
@ -289,6 +289,31 @@ class DocumentTest(unittest.TestCase):
|
|||||||
Zoo.drop_collection()
|
Zoo.drop_collection()
|
||||||
Animal.drop_collection()
|
Animal.drop_collection()
|
||||||
|
|
||||||
|
def test_reference_inheritance(self):
|
||||||
|
class Stats(Document):
|
||||||
|
created = DateTimeField(default=datetime.now)
|
||||||
|
|
||||||
|
meta = {'allow_inheritance': False}
|
||||||
|
|
||||||
|
class CompareStats(Document):
|
||||||
|
generated = DateTimeField(default=datetime.now)
|
||||||
|
stats = ListField(ReferenceField(Stats))
|
||||||
|
|
||||||
|
Stats.drop_collection()
|
||||||
|
CompareStats.drop_collection()
|
||||||
|
|
||||||
|
list_stats = []
|
||||||
|
|
||||||
|
for i in xrange(10):
|
||||||
|
s = Stats()
|
||||||
|
s.save()
|
||||||
|
list_stats.append(s)
|
||||||
|
|
||||||
|
cmp_stats = CompareStats(stats=list_stats)
|
||||||
|
cmp_stats.save()
|
||||||
|
|
||||||
|
self.assertEqual(list_stats, CompareStats.objects.first().stats)
|
||||||
|
|
||||||
def test_inheritance(self):
|
def test_inheritance(self):
|
||||||
"""Ensure that document may inherit fields from a superclass document.
|
"""Ensure that document may inherit fields from a superclass document.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user