
from bson import ObjectId
from mongoengine.fields import MapField
import six


__all__ = ('ObjectIdMapField')


class ObjectIdMapField(MapField):
    '''Similar to a MongoEngine `MapField`, but where keys are `ObjectId`s instead
    of strings'''

    def validate(self, value):
        """Make sure that a list of valid fields is being used."""
        if not isinstance(value, dict):
            self.error('Only dictionaries may be used as the value for a ObjectIdMapField')

        if not all(map(lambda k: isinstance(k, ObjectId), value.keys())):
            msg = 'Invalid key in `ObjectIdMapField` - keys must be `ObjectId`s.'
            self.error(msg)
        super(MapField, self).validate({str(key): val for key, val in six.iteritems(value)})

    def to_python(self, value):
        '''Convert a MongoDB-compatible type to a Python type.'''
        self.field._auto_dereference = self._auto_dereference
        return {ObjectId(key): self.field.to_python(item) for key, item in value.items()}

    def to_mongo(self, value, use_db_field=True, fields=None):
        return {
            str(key): self.field._to_mongo_safe_call(item, use_db_field, fields)
            for key, item in six.iteritems(value)
        }

@@ -106,16 +106,416 @@
 eld')%0A%0A%0A
+def _to_mongo_safe_call(instance, value, use_db_field=True, fields=None):%0A    '''MongoEngine helper method introduced in v0.10.7'''%0A    f_inputs = instance.to_mongo.__code__.co_varnames%0A    ex_vars = %7B%7D%0A    if 'fields' in f_inputs:%0A        ex_vars%5B'fields'%5D = fields%0A%0A    if 'use_db_field' in f_inputs:%0A        ex_vars%5B'use_db_field'%5D = use_db_field%0A%0A    return instance.to_mongo(value, **ex_vars)%0A%0A%0A
 class Ob
@@ -1504,27 +1504,16 @@
 r(key): 
-self.field.
 _to_mong
@@ -1524,16 +1524,28 @@
 fe_call(
+self.field, 
 item, us

Add support for MongoEngine v0.10.6