From 12a7fc1af18173d2bdcbd59f84a639dd832f72bd Mon Sep 17 00:00:00 2001 From: blackbrrr Date: Fri, 18 Dec 2009 11:34:32 -0600 Subject: [PATCH] removed reliance on '_cls' in document; fields only parsed if '__class__' present, allowing inner classes and non-field attributes on a document --- mongoengine/base.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index 61deb409..6cb453c1 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -111,7 +111,8 @@ class DocumentMetaclass(type): # Add the document's fields to the _fields attribute for attr_name, attr_value in attrs.items(): - if issubclass(attr_value.__class__, BaseField): + if hasattr(attr_value, "__class__") and \ + issubclass(attr_value.__class__, BaseField): if not attr_value.name: attr_value.name = attr_name doc_fields[attr_name] = attr_value @@ -135,7 +136,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass): if attrs.get('__metaclass__') == TopLevelDocumentMetaclass: return super_new(cls, name, bases, attrs) - collection = name.lower() + collection = attrs.get('__collection__', name.lower()) + # Subclassed documents inherit collection from superclass for base in bases: if hasattr(base, '_meta') and 'collection' in base._meta: @@ -233,9 +235,14 @@ class BaseDocument(object): def _from_son(cls, son): """Create an instance of a Document (subclass) from a PyMongo SOM. """ - class_name = son[u'_cls'] + # get the class name from the document, falling back to the given + # class if unavailable + class_name = son.get(u'_cls', cls._class_name) + data = dict((str(key), value) for key, value in son.items()) - del data['_cls'] + + if '_cls' in data: + del data['_cls'] # Return correct subclass for document type if class_name != cls._class_name: