how to get mongoengine object id in flask program

0 votes

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) 

Oct 10, 2018 in Python by eatcodesleeprepeat
• 4,710 points
786 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 Oct 10, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

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

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

answered Sep 21, 2018 in Python by charlie_brown
• 7,720 points
3,330 views
+1 vote
1 answer

How to get the currently logged in user's user id in Django?

Hello @kartik, First make sure you have SessionMiddleware and AuthenticationMiddleware middlewares added ...READ MORE

answered Aug 12, 2020 in Python by Niroj
• 82,840 points
37,158 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,414 views
0 votes
1 answer

How to get the current time in Python

>>> import datetime >>> datetime.datetime.now() datetime(2018, 25, ...READ MORE

answered Jul 25, 2018 in Python by Frankie
• 9,830 points
799 views
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,172 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
3 answers

How to get the current time in Python

FOLLOWING WAY TO FIND CURRENT TIME IN ...READ MORE

answered Apr 8, 2019 in Python by rajesh
• 1,270 points
2,100 views
0 votes
1 answer

How to convert date string to date object in python?

Use this :-  >>> datetime.datetime.strptime('24052010', "%d%m%Y").date() datetime.date(2010, 5, 24) Hope ...READ MORE

answered Oct 22, 2018 in Python by Priyaj
• 58,020 points
11,253 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