Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def lookup_member(self, member_name):
return self.document_type._fields.get(member_name)

def prepare_query_value(self, op, value):
if not isinstance(value, self.document_type):
if value is not None and not isinstance(value, self.document_type):
value = self.document_type._from_son(value)
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
return self.to_mongo(value)
Expand Down
24 changes: 20 additions & 4 deletions tests/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,8 @@ class BlogPost(Document):
self.assertFalse('$orderby' in q.get_ops()[0]['query'])

def test_find_embedded(self):
"""Ensure that an embedded document is properly returned from a query.
"""Ensure that an embedded document is properly returned from
a query.
"""
class User(EmbeddedDocument):
name = StringField()
Expand All @@ -1250,16 +1251,31 @@ class BlogPost(Document):

BlogPost.drop_collection()

post = BlogPost(content='Had a good coffee today...')
post.author = User(name='Test User')
post.save()
BlogPost.objects.create(
author=User(name='Test User'),
content='Had a good coffee today...'
)

result = BlogPost.objects.first()
self.assertTrue(isinstance(result.author, User))
self.assertEqual(result.author.name, 'Test User')

def test_find_empty_embedded(self):
"""Ensure that you can save and find an empty embedded document."""
class User(EmbeddedDocument):
name = StringField()

class BlogPost(Document):
content = StringField()
author = EmbeddedDocumentField(User)

BlogPost.drop_collection()

BlogPost.objects.create(content='Anonymous post...')

result = BlogPost.objects.get(author=None)
self.assertEqual(result.author, None)

def test_find_dict_item(self):
"""Ensure that DictField items may be found.
"""
Expand Down