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:
parent
5e6a6aa886
commit
c58f377a0a
@ -111,7 +111,8 @@ class DocumentMetaclass(type):
|
|||||||
|
|
||||||
# Add the document's fields to the _fields attribute
|
# Add the document's fields to the _fields attribute
|
||||||
for attr_name, attr_value in attrs.items():
|
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:
|
if not attr_value.name:
|
||||||
attr_value.name = attr_name
|
attr_value.name = attr_name
|
||||||
doc_fields[attr_name] = attr_value
|
doc_fields[attr_name] = attr_value
|
||||||
@ -135,7 +136,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
if attrs.get('__metaclass__') == TopLevelDocumentMetaclass:
|
if attrs.get('__metaclass__') == TopLevelDocumentMetaclass:
|
||||||
return super_new(cls, name, bases, attrs)
|
return super_new(cls, name, bases, attrs)
|
||||||
|
|
||||||
collection = name.lower()
|
collection = attrs.get('__collection__', name.lower())
|
||||||
|
|
||||||
# Subclassed documents inherit collection from superclass
|
# Subclassed documents inherit collection from superclass
|
||||||
for base in bases:
|
for base in bases:
|
||||||
if hasattr(base, '_meta') and 'collection' in base._meta:
|
if hasattr(base, '_meta') and 'collection' in base._meta:
|
||||||
@ -234,9 +236,14 @@ class BaseDocument(object):
|
|||||||
def _from_son(cls, son):
|
def _from_son(cls, son):
|
||||||
"""Create an instance of a Document (subclass) from a PyMongo SOM.
|
"""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())
|
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
|
# Return correct subclass for document type
|
||||||
if class_name != cls._class_name:
|
if class_name != cls._class_name:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user