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,460 points
3,266 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,143 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,020 points
751 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,460 points
1,370 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,445 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,827 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,832 views
0 votes
4 answers
0 votes
1 answer

How to get permutations of list or a set in Python?

Permutation is an arrangement of objects in ...READ MORE

answered Jul 11, 2019 in Python by Neel
• 3,020 points
8,892 views
0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,056 views
0 votes
1 answer

How to round a floating point number up to certain decimal place in Python?

This is normal (and has nothing to do ...READ MORE

answered Oct 8, 2018 in Python by charlie_brown
• 7,720 points
2,357 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP