Answer the question
In order to leave comments, you need to log in
How to add row to database using insert method in flask-mongoalchemy?
Hello everyone
I started to learn mongodb from this guy's video
https://www.youtube.com/watch?v=vVx1737auSE
I
created a database on https://mlab.com
, it connects with this code without any problems, the login and password are correct.
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE'] = 'dbname'
app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://username:[email protected]:31315/bestpricedb'
db = MongoAlchemy(app)
class User(db.Document):
name = db.StringField()
class query(db.Document):
qeryz = db.StringField()
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE'] = 'dbname'
app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://username:[email protected]:31315/bestpricedb'
mongo = MongoAlchemy(app)
@app.route('/add')
def add():
user = mongo.db.users
user.insert({'name' : 'Lega'})
return ('User add')
if __name__ == '__main__':
app.run(debug=True)
Traceback (most recent call last)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\PythonServer\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\PythonServer\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\waidones\Envs\bestprice\mongo_connect.py", line 13, in add
user = mongo.db.users
AttributeError: 'MongoAlchemy' object has no attribute 'db'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question