Fixed EmbeddedDocuments with id also storing _id (#402)

This commit is contained in:
Ross Lawley 2013-07-11 08:11:00 +00:00
parent 6c599ef506
commit d593f7e04b
2 changed files with 11 additions and 2 deletions

View File

@ -262,6 +262,8 @@ class BaseDocument(object):
data[field.db_field] = value
# If "_id" has not been set, then try and set it
Document = _import_class("Document")
if isinstance(self, Document):
if data["_id"] is None:
data["_id"] = self._data.get("id", None)

View File

@ -444,6 +444,13 @@ class InstanceTest(unittest.TestCase):
self.assertEqual(Employee(name="Bob", age=35, salary=0).to_mongo().keys(),
['_cls', 'name', 'age', 'salary'])
def test_embedded_document_to_mongo_id(self):
class SubDoc(EmbeddedDocument):
id = StringField(required=True)
sub_doc = SubDoc(id="abc")
self.assertEqual(sub_doc.to_mongo().keys(), ['id'])
def test_embedded_document(self):
"""Ensure that embedded documents are set up correctly.
"""