Skip to content

Commit

Permalink
fix for issue MongoEngine#904, also includes a test which checks the …
Browse files Browse the repository at this point in the history
…error case I reported in the issue report.
  • Loading branch information
ms5 committed Mar 8, 2016
1 parent 6d3bc43 commit 4af78d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions tests/document/db_fields.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4af78d6

Please sign in to comment.