Merge pull request #1267 from wishtack/hotfix-map-field-unicode-key

Fix MapField in order to handle unicode keys.
This commit is contained in:
Omer Katz 2016-03-26 09:06:24 +03:00
commit 7ad7b08bed
2 changed files with 26 additions and 1 deletions

View File

@ -1,5 +1,7 @@
from bson import DBRef, SON from bson import DBRef, SON
from mongoengine.python_support import txt_type
from base import ( from base import (
BaseDict, BaseList, EmbeddedDocumentList, BaseDict, BaseList, EmbeddedDocumentList,
TopLevelDocumentMetaclass, get_document TopLevelDocumentMetaclass, get_document
@ -226,7 +228,7 @@ class DeReference(object):
data[k]._data[field_name] = self.object_map.get( data[k]._data[field_name] = self.object_map.get(
(v['_ref'].collection, v['_ref'].id), v) (v['_ref'].collection, v['_ref'].id), v)
elif isinstance(v, (dict, list, tuple)) and depth <= self.max_depth: elif isinstance(v, (dict, list, tuple)) and depth <= self.max_depth:
item_name = "{0}.{1}.{2}".format(name, k, field_name) item_name = txt_type("{0}.{1}.{2}").format(name, k, field_name)
data[k]._data[field_name] = self._attach_objects(v, depth, instance=instance, name=item_name) data[k]._data[field_name] = self._attach_objects(v, depth, instance=instance, name=item_name)
elif isinstance(v, (dict, list, tuple)) and depth <= self.max_depth: elif isinstance(v, (dict, list, tuple)) and depth <= self.max_depth:
item_name = '%s.%s' % (name, k) if name else name item_name = '%s.%s' % (name, k) if name else name

View File

@ -1539,6 +1539,29 @@ class FieldTest(unittest.TestCase):
actions__friends__operation='drink', actions__friends__operation='drink',
actions__friends__object='beer').count()) actions__friends__object='beer').count())
def test_map_field_unicode(self):
class Info(EmbeddedDocument):
description = StringField()
value_list = ListField(field=StringField())
class BlogPost(Document):
info_dict = MapField(field=EmbeddedDocumentField(Info))
BlogPost.drop_collection()
tree = BlogPost(info_dict={
u"éééé": {
'description': u"VALUE: éééé"
}
})
tree.save()
self.assertEqual(BlogPost.objects.get(id=tree.id).info_dict[u"éééé"].description, u"VALUE: éééé")
BlogPost.drop_collection()
def test_embedded_db_field(self): def test_embedded_db_field(self):
class Embedded(EmbeddedDocument): class Embedded(EmbeddedDocument):