From 4af78d601623ef6b2f0609340797480fa2c85032 Mon Sep 17 00:00:00 2001 From: Manuel Schraner Date: Tue, 8 Mar 2016 06:00:40 +0100 Subject: [PATCH] fix for issue #904, also includes a test which checks the error case I reported in the issue report. --- mongoengine/base/document.py | 7 ++++++- tests/document/db_fields.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/document/db_fields.py diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 3eba16ca8..d683e54cb 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -686,7 +686,12 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False) # get the class name from the document, falling back to the given # class if unavailable class_name = son.get('_cls', cls._class_name) - data = dict(("%s" % key, value) for key, value in son.iteritems()) + if PY3: + son_gen = son.items() + else: + son_gen = son.iteritems() + + data = dict(("%s" % cls._db_field_map.get(key,key), value) for key, value in son_gen) # Return correct subclass for document type if class_name != cls._class_name: diff --git a/tests/document/db_fields.py b/tests/document/db_fields.py new file mode 100644 index 000000000..d2a0abdea --- /dev/null +++ b/tests/document/db_fields.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +import sys +sys.path[0:0] = [""] +import unittest +from mongoengine import * + +__all__ = ("DbFieldParameterTest", ) + + +class DbFieldParameterTest(unittest.TestCase): + + def test_document_db_field_validation(self): + + # https://github.com/MongoEngine/mongoengine/issues/904 + + data = {'b': {'c': [{'x': 1.0, 'y': 2.0}]}} + + class C(EmbeddedDocument): + x = FloatField(db_field='fx') + y = FloatField(db_field='fy') + + class B(EmbeddedDocument): + c = ListField(EmbeddedDocumentField(C), db_field='fc') + + class A(Document): + b = EmbeddedDocumentField(B, db_field='fb') + + a = A(**data) + + try: + a.validate() + except ValidationError as e: + self.fail("ValidationError raised: %s" % e.message) + + +if __name__ == '__main__': + unittest.main()