Mongoengine query using only () on a list of referenced fields
I have two documents
class Book(Document):
title = StringField()
publication = StringField()
no_of_pages = IntField()
desc = StringField()
reviews = ListField(ReferenceField(Review))
...
class Author(Document):
name = StringField()
books = ListField(ReferenceField(Book))
desc = StringField()
...
I want to write a query that gives the name of the author, finds all of his book tiles.
As you can see there are many columns in both documents, I don't want to get all columns. so my required fields are
`req_fields = ['name', 'books']`
and I am requesting it like this
author = Author.objects.filter(name='xyz').only(*req_fields)
If I print an object author
, I get:
{ _id=ObjectId('50fcf9682c345a0427000019'), _cls='Author', name='xyz', books=[DBRef('Book', ObjectId('50fcd9682c441e0427000019')), DBRef('Book', ObjectId('50fcd9682c361e0427000020'))] }
I would like the query to return a list of strings for the title, not a list of book objects. How can I change this? so the result objects look like this:
{ _id=ObjectId('50fcf9682c345a0427000019'), _cls='Author', name='xyz', books=['Title One', 'Title Two'] }
+3
source to share