How to dereference DBRef to a Document, after distinct operation?

1,027 views
Skip to first unread message

Tyr

unread,
Mar 1, 2011, 3:40:10 AM3/1/11
to MongoEngine Users
Hi,
I have a query which do distinct operation on a ReferenceField, like
this:

cls.objects(q).distinct('obj') # obj is a GenericReferenceField

I got a list of DBRef for this query. Although db.dereference() can
get the required data for me, it is a dictionary, not a Document. Does
anyone know how to dereference DBRef to a Document?

Thanks,
Tyr

Gaarv

unread,
Mar 9, 2011, 7:45:45 AM3/9/11
to MongoEngine Users
Seems like the same I had to do for a recent internal project. I
removed some elements, ie. ".only()" in order to be closer from your
example :

# get the list of IDs
id_list = [i.id for i in Checklist.objects.distinct('chk_type')]
# dereference them and get the document for each
checklist_query = Checklist_Type.objects.in_bulk(id_list).values()

chk_type is a ReferenceField to Checklist_Type.

Regards,
Gaarv
Reply all
Reply to author
Forward
Message has been deleted
0 new messages