removed reliance on '_cls' in document; fields only parsed if '__class__' present, allowing inner classes and non-field attributes on a document

This commit is contained in:
blackbrrr 2009-12-19 01:34:32 +08:00 committed by hmarr
parent 5e6a6aa886
commit c58f377a0a

View File

@ -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:
@ -234,8 +236,13 @@ 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())
if '_cls' in data:
del data['_cls']
# Return correct subclass for document type