From 3477b0107a227b88a1936fb23fa24ad7b26524a6 Mon Sep 17 00:00:00 2001 From: Loic Raucy Date: Tue, 26 Feb 2013 11:12:37 +0100 Subject: [PATCH 1/2] Added regression test for numerical string keys. --- tests/test_fields.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index 28af1b23..47669007 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -965,6 +965,24 @@ class FieldTest(unittest.TestCase): doc = self.db.test.find_one() self.assertEqual(doc['x']['DICTIONARY_KEY']['i'], 2) + def test_mapfield_numerical_index(self): + """Ensure that MapField accept numeric strings as indexes.""" + class Embedded(EmbeddedDocument): + name = StringField() + + class Test(Document): + my_map = MapField(EmbeddedDocumentField(Embedded)) + + Test.drop_collection() + + test = Test() + test.my_map['1'] = Embedded(name='test') + test.save() + test.my_map['1'].name = 'test updated' + test.save() + + Test.drop_collection() + def test_map_field_lookup(self): """Ensure MapField lookups succeed on Fields without a lookup method""" From d0245bb5ba3b0f4ca4ce654fd199c167ce8c5e96 Mon Sep 17 00:00:00 2001 From: Loic Raucy Date: Tue, 26 Feb 2013 11:14:47 +0100 Subject: [PATCH 2/2] Fixed #238: dictfields handle numerical strings indexes. --- mongoengine/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index 013afe78..4f302a87 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -1193,7 +1193,7 @@ class BaseDocument(object): for p in parts: if isinstance(d, DBRef): break - elif p.isdigit(): + elif isinstance(d, list) and p.isdigit(): d = d[int(p)] elif hasattr(d, 'get'): d = d.get(p) @@ -1224,7 +1224,7 @@ class BaseDocument(object): parts = path.split('.') db_field_name = parts.pop() for p in parts: - if p.isdigit(): + if isinstance(d, list) and p.isdigit(): d = d[int(p)] elif (hasattr(d, '__getattribute__') and not isinstance(d, dict)):