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 ariaholic
• 7,320 points
31 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,710 points

Related Questions In Python

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 5, 2018 in Python by Priyaj
• 56,120 points
362 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
• 56,120 points
36 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 ariaholic
• 7,320 points
48 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,500 points
2,479 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,500 points
2,863 views
0 votes
1 answer

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

def add(a,b):      return a + b #when i call ...READ MORE

answered 5 days ago in Python by Mohammad
• 1,400 points
8 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
424 views
0 votes
1 answer

How to hide a window in the constructor immediately after creation?

You can use QtCore.QTimer class Example(QWidget):     def __init__(self, app):         QWidget.__init__(self)         QTimer.singleShot(0, ...READ MORE

answered Apr 17, 2018 in Python by anonymous
16 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,710 points
38 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,710 points
20 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.