To achive storing object data in order with minimum implementation, I
changed followings. - added optional parameter `container_class` which enables to choose intermediate class at encoding Python data, instead of additional field class. - removed OrderedDocument class because the equivalent feature could be implemented by the outside of Mongoengine.
This commit is contained in:
@@ -5,7 +5,6 @@ import re
|
||||
import time
|
||||
import uuid
|
||||
import warnings
|
||||
from collections import OrderedDict
|
||||
from operator import itemgetter
|
||||
|
||||
from bson import Binary, DBRef, ObjectId, SON
|
||||
@@ -50,7 +49,7 @@ __all__ = (
|
||||
'FileField', 'ImageGridFsProxy', 'ImproperlyConfigured', 'ImageField',
|
||||
'GeoPointField', 'PointField', 'LineStringField', 'PolygonField',
|
||||
'SequenceField', 'UUIDField', 'MultiPointField', 'MultiLineStringField',
|
||||
'MultiPolygonField', 'GeoJsonBaseField', 'OrderedDynamicField'
|
||||
'MultiPolygonField', 'GeoJsonBaseField'
|
||||
)
|
||||
|
||||
RECURSIVE_REFERENCE_CONSTANT = 'self'
|
||||
@@ -620,6 +619,14 @@ class DynamicField(BaseField):
|
||||
|
||||
Used by :class:`~mongoengine.DynamicDocument` to handle dynamic data"""
|
||||
|
||||
def __init__(self, container_class=dict, *args, **kwargs):
|
||||
self._container_cls = container_class
|
||||
if not issubclass(self._container_cls, dict):
|
||||
self.error('The class that is specified in `container_class` parameter '
|
||||
'must be a subclass of `dict`.')
|
||||
|
||||
super(DynamicField, self).__init__(*args, **kwargs)
|
||||
|
||||
def to_mongo(self, value, use_db_field=True, fields=None):
|
||||
"""Convert a Python type to a MongoDB compatible type.
|
||||
"""
|
||||
@@ -645,7 +652,7 @@ class DynamicField(BaseField):
|
||||
is_list = True
|
||||
value = {k: v for k, v in enumerate(value)}
|
||||
|
||||
data = self._container_type() if hasattr(self, '_container_type') else {}
|
||||
data = self._container_cls()
|
||||
for k, v in value.iteritems():
|
||||
data[k] = self.to_mongo(v, use_db_field, fields)
|
||||
|
||||
@@ -676,16 +683,6 @@ class DynamicField(BaseField):
|
||||
value.validate(clean=clean)
|
||||
|
||||
|
||||
class OrderedDynamicField(DynamicField):
|
||||
"""A field that wraps DynamicField. This uses OrderedDict class
|
||||
to guarantee to store data in the defined order instead of dict.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(OrderedDynamicField, self).__init__(*args, **kwargs)
|
||||
self._container_type = OrderedDict
|
||||
|
||||
|
||||
class ListField(ComplexBaseField):
|
||||
"""A list field that wraps a standard field, allowing multiple instances
|
||||
of the field to be used as a list in the database.
|
||||
|
Reference in New Issue
Block a user