Fix filtering by embedded_doc=None (#1422)

This commit is contained in:
Stefan Wójcik 2016-12-04 00:32:53 -05:00 committed by GitHub
parent 800f656dcf
commit 8391af026c
2 changed files with 21 additions and 5 deletions

View File

@ -577,7 +577,7 @@ class EmbeddedDocumentField(BaseField):
return self.document_type._fields.get(member_name) return self.document_type._fields.get(member_name)
def prepare_query_value(self, op, value): 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) value = self.document_type._from_son(value)
super(EmbeddedDocumentField, self).prepare_query_value(op, value) super(EmbeddedDocumentField, self).prepare_query_value(op, value)
return self.to_mongo(value) return self.to_mongo(value)

View File

@ -1239,7 +1239,8 @@ class QuerySetTest(unittest.TestCase):
self.assertFalse('$orderby' in q.get_ops()[0]['query']) self.assertFalse('$orderby' in q.get_ops()[0]['query'])
def test_find_embedded(self): 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): class User(EmbeddedDocument):
name = StringField() name = StringField()
@ -1250,16 +1251,31 @@ class QuerySetTest(unittest.TestCase):
BlogPost.drop_collection() BlogPost.drop_collection()
post = BlogPost(content='Had a good coffee today...') BlogPost.objects.create(
post.author = User(name='Test User') author=User(name='Test User'),
post.save() content='Had a good coffee today...'
)
result = BlogPost.objects.first() result = BlogPost.objects.first()
self.assertTrue(isinstance(result.author, User)) self.assertTrue(isinstance(result.author, User))
self.assertEqual(result.author.name, 'Test 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.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): def test_find_dict_item(self):
"""Ensure that DictField items may be found. """Ensure that DictField items may be found.
""" """