Merge pull request #583 from Gerrrr/distinct_bug
Fixed bug in distinct method
This commit is contained in:
commit
c3f8925f46
@ -717,7 +717,10 @@ class BaseQuerySet(object):
|
||||
# We may need to cast to the correct type eg. ListField(EmbeddedDocumentField)
|
||||
doc_field = getattr(self._document._fields.get(field), "field", None)
|
||||
instance = getattr(doc_field, "document_type", False)
|
||||
if instance:
|
||||
EmbeddedDocumentField = _import_class('EmbeddedDocumentField')
|
||||
GenericEmbeddedDocumentField = _import_class('GenericEmbeddedDocumentField')
|
||||
if instance and isinstance(doc_field, (EmbeddedDocumentField,
|
||||
GenericEmbeddedDocumentField)):
|
||||
distinct = [instance(**doc) for doc in distinct]
|
||||
return distinct
|
||||
|
||||
|
@ -2735,6 +2735,27 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
self.assertEqual(authors, [mark_twain, john_tolkien])
|
||||
|
||||
def test_distinct_ListField_ReferenceField(self):
|
||||
class Foo(Document):
|
||||
bar_lst = ListField(ReferenceField('Bar'))
|
||||
|
||||
class Bar(Document):
|
||||
text = StringField()
|
||||
|
||||
Bar.drop_collection()
|
||||
Foo.drop_collection()
|
||||
|
||||
bar_1 = Bar(text="hi")
|
||||
bar_1.save()
|
||||
|
||||
bar_2 = Bar(text="bye")
|
||||
bar_2.save()
|
||||
|
||||
foo = Foo(bar=bar_1, bar_lst=[bar_1, bar_2])
|
||||
foo.save()
|
||||
|
||||
self.assertEqual(Foo.objects.distinct("bar_lst"), [bar_1, bar_2])
|
||||
|
||||
def test_custom_manager(self):
|
||||
"""Ensure that custom QuerySetManager instances work as expected.
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user