Answer the question
In order to leave comments, you need to log in
How to get Reference Field from MongoDB using Flas\MongEngine?
Hello!
There is a problem:
With the help of MongoEngine, I created a "scheme" of documents:
class Users(Document):
login = fields.StringField(unique=True, max_length=25, primary_key=True, required=True)
books = fields.ListField(fields.ReferenceField('Books'), default=["No favorites"])
class Books(Document):
title = fields.StringField(required=True)
author = fields.StringField()
@app.route('/users/<id>', methods=['GET'])
def edit_user(id):
user = Users.objects(login=id)
books = [Books.objects(id__in=user[0].books)]
return user.to_json(), books.to_json()
ValidationError: Books object is not a valid ObjectId
Answer the question
In order to leave comments, you need to log in
Um, I’m not boom-boom in Python, especially in MongoEngine, but by analogy with other systems, user[0].books will return your books to you by independently executing the request that you are trying to execute by id manually.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question