How to get a Mongoengine s object id in a flask program

0 votes

Hey guys, I am using mongoengine to integrate with flask , i wanted to know how to get document object id every time i try i get File "/var/www/flask_projects/iot_mongo/app.py", line 244, in post return jsonify(user.id) AttributeError: 'BaseQuerySet' object has no attribute 'id'

class Test(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('email',required=True, help='email')
        args=parser.parse_args()
        user=AdminUser.objects(email=args['email'])
        return jsonify(user.id)
api.add_resource(Test,'/test')

if __name__ == '__main__':
    app.run(debug=True) 
Sep 21, 2018 in Python by aryya
• 7,450 points
2,971 views

1 answer to this question.

0 votes

Check out the documentationDocument.objects is a QuerySet object.

You seem to be expecting that this part of your code

user=AdminUser.objects(email=args['email']) # user will be a QuerySet

will give you a single result, which is not the case, it will give you a QuerySet with zero or more results. It does not have an attribute id, this is why you get the error message you are seeing when you try to access this attribute here:

return jsonify(user.id) # QuerySet does not have the attribute id

You need to fetch the actual result(s) you want from it, assuming you are sure your query will return a single result, or do not care that there might be more than one result and just want the first one, you probably want something along these lines:

user=AdminUser.objects(email=args['email']).first() # extract first result
return jsonfiy(user)

Alernatively, returning all results would look like this:

users=AdminUser.objects(email=args['email']).all() # extract all results
return jsonfiy(users)
answered Sep 21, 2018 by charlie_brown
• 7,720 points

Related Questions In Python

0 votes
0 answers

how to get mongoengine object id in flask program

i am using mongoengine to integrate with ...READ MORE

Oct 5, 2018 in Python by eatcodesleeprepeat
• 4,710 points
6,048 views
0 votes
1 answer

how to get mongoengine object id in flask program

Check out the documentation, Document.objects is a QuerySet object. You seem to be ...READ MORE

answered Oct 10, 2018 in Python by Priyaj
• 58,090 points
654 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
1,190 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,560 points
34,236 views
0 votes
1 answer

How do you add a background thread to flask in Python?

The example below creates a background thread ...READ MORE

answered Nov 19, 2018 in Python by Nymeria
• 3,560 points
36,513 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,545 views
0 votes
4 answers
0 votes